scale_fill_manual is not working in geom_bar
Asked Answered
M

1

7

I'm trying to change the color in a barplot in ggplot2 using scale_fill_manual, but for some reason only works if i use the fill option inside aesthetics. I made an example:

library(ggplot2)
library(dplyr) 
iris %>% ggplot(aes(x=Sepal.Width,y=Sepal.Length))+
geom_bar(stat="identity") + scale_fill_manual(values='lightblue')

Here is the result, no changing in the color: enter image description here

Now, using the fill option inside aesthetics, it works:

iris %>% ggplot(aes(x=Sepal.Width,y=Sepal.Length, fill=factor(2) ))+
geom_bar(stat="identity")+scale_fill_manual(values='lightblue')

enter image description here

There is some way to change the bar colour without using the fill option, only using scale_fill_manual?

Medicine answered 18/7, 2016 at 18:22 Comment(8)
scale_fill_manual will work only if you have a fill aesthetic inside aes. But if you don't want to map a data column to the fill aesthetic, why not just set the fill color inside geom_bar: geom_bar(stat="identity", fill="lightblue")?Apprehensive
Remember that aes() maps aesthetics, but you can set them using normal function arguments.Tilt
I have the same question. Let's say I am creating a "theme" and want the default color of the bars to be blue. I want to theme to automate that, even if the user has not declared a fill variable.Proboscis
I posted a similar question here: github.com/jrnold/ggthemes/issues/67 (no answer yet though)Proboscis
@LucasMation, does this SO answer answer your question?Apprehensive
@eipi10, Awesome! Tks!Proboscis
@eipi10, for this example your suggestion works fine, but my goal is change de color without using a fill option, in any part of the code. That's because i've already a personal pallete, so my ideia is that the user doesn't have to worry about choosing any color, he will only have to add "+ personal_pallete" in the script, where: cbPalette <- c('#003576','#006EAB','#9CD2EB','#000000','#7D7D7D','#C8C8C8') personal_pallete<- list(scale_fill_manual(values=cbPalette),scale_colour_manual(values=cbPalette)). That's why i need to change the color only using scale_fill_manual. Any suggestion?Medicine
Sorry @eipi10, didn't see your suggestion to LucasMation, i'm gonna try this.Medicine
F
4

You need to define fill inside your aes .

Fresco answered 1/12, 2017 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.