Home > Blockchain >  multicolor bar chart ggplot
multicolor bar chart ggplot

Time:09-22

I am trying have a multicolor bar chart, but scale_fill_manual or scale_color_brewer etc. do neither work outside or inside geom_bar -> Can sb help, please?

ggplot(data=sust_future, aes(x=as.factor(`sust.self-perception`)))   
  geom_bar(aes(y = (..count..)/sum(..count..)),width=0.6)    
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))  
  scale_y_continuous(labels=scales::percent)   
  labs(title = "xxx", x = "", y = "")  
  theme(legend.position = "bottom",
        axis.text.x=element_text(size=8),
        axis.text.y=element_text(size=8)) 

enter image description here

CodePudding user response:

I'm not sure how you want the bars to be coloured (for example based on a factor in a column or based on some other conditions). If you are trying to colour your geom_bar based on something in your data, you'd have to define how you'd like to colour in your bars via aes(fill = column_name) either globally in ggplot() or specifically in geom_bar(). Here is an example you can try to implement in your case (as I do not know what is in sust_future:

# global setting
ggplot(iris, aes(x = Species, fill = Species))  
  geom_bar() 
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))

# local to geom_bar
ggplot(iris, aes(x = Species))  
  geom_bar(aes(fill = Species)) 
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
  • Related