6

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?

Roberto
  • 93
  • 1
  • 1
  • 4
  • 9
    `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")`? – eipi10 Jul 18 '16 at 18:50
  • Remember that `aes()` _maps_ aesthetics, but you can _set_ them using normal function arguments. – Axeman Jul 18 '16 at 18:58
  • 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. – LucasMation Jul 18 '16 at 19:03
  • I posted a similar question here: https://github.com/jrnold/ggthemes/issues/67 (no answer yet though) – LucasMation Jul 18 '16 at 19:05
  • 2
    @LucasMation, does [this SO answer](http://stackoverflow.com/a/21175042/496488) answer your question? – eipi10 Jul 18 '16 at 19:16
  • @eipi10, Awesome! Tks! – LucasMation Jul 18 '16 at 19:21
  • @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? – Roberto Jul 18 '16 at 21:53
  • Sorry @eipi10, didn't see your suggestion to LucasMation, i'm gonna try this. – Roberto Jul 18 '16 at 22:04

1 Answers1

4

You need to define fill inside your aes .

ABCD
  • 7,914
  • 9
  • 54
  • 90