Setting fill colour of bars manually

Hi,

I'm trying to set fill colours of bars manually but the task is more difficult than I expected. Could you help me, please?

Many thanks,

Jakkub

library(tidyverse)

table <-  tibble("value"=c(0.6, 0.3, 0.08,0.02 ),
                   "group"= c("Surely yes","Probably yes","Probably no", "Surely no"))   

ggplot(data = table,
       mapping = aes(x = reorder(group, -value),  y = value,label = scales::percent(value))) + 
  geom_col()+
  scale_y_continuous(labels=scales::percent)+
  labs(title="Odhodlání jít volit", x="",y="")+
  geom_text(position = position_dodge(width = .9),    # move to center of bars
            vjust = -0.5,    # nudge above top of bar
            size = 3)+
  theme(panel.background = element_blank(),
        axis.text.x=element_text(size=9,face="bold"),
        axis.text.y=element_text(size=9, face="bold"))+
  scale_fill_manual(values=c("red","grey","black","orange"))

image

Created on 2021-02-08 by the reprex package (v0.3.0)

I just added fill = group in the aes section.

library(tidyverse)

table <-  tibble("value"=c(0.6, 0.3, 0.08,0.02 ),
                 "group"= c("Surely yes","Probably yes","Probably no", "Surely no"))   

ggplot(data = table,
       mapping = aes(x = reorder(group, -value),  y = value, label = scales::percent(value), fill = group)) + 
  geom_col()+
  scale_y_continuous(labels=scales::percent)+
  labs(title="Odhodlání jít volit", x="",y="")+
  geom_text(position = position_dodge(width = .9),    # move to center of bars
            vjust = -0.5,    # nudge above top of bar
            size = 3)+
  theme(panel.background = element_blank(),
        axis.text.x=element_text(size=9,face="bold"),
        axis.text.y=element_text(size=9, face="bold"))+
  scale_fill_manual(values=c("red","grey","black","orange"))

Can you post an example of your dataset preference?

It might be that fill needs to be inside the aes().

Sorry, I spotted a mistake. Do you know a way how to set colours so that I could make compliacted plots? That everytime I plot 'surely yes' it would be orange even though the order would be different?

You can used a named vector (the second example) here:

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.