ggplot2: geom_bar stacked barplot, specify bar outline color

CAT picture CAT · Oct 9, 2014 · Viewed 7.5k times · Source

I am trying to figure out how to specify the outline color on a stacked barplot in ggplot2. In the below code I specify color="green", which gives a green outline to each of the bars. I would like to specify a different outline color for each bar (e.g. cut=Fair would be filled with yellow and outlined with orange, cut=Good would be filled with light green and outlined with dark green, etc.).

ggplot(diamonds) +  
  geom_bar(aes(clarity, fill=cut))+
  scale_fill_manual(values=c("Fair"="yellow","Good"="light green","Very Good"="light blue","Premium"="pink","Ideal"="purple"))+

I have tried scale_color_manual() and specifying a vector of colors in the geom_bar() aesthetics, neither have worked.

Answer

nograpes picture nograpes · Oct 9, 2014

You must map both aesthetics to the cut variable, and then you can use scale_colour_manual. Here is an (ugly) example:

ggplot(diamonds) +  
  geom_bar(aes(clarity, fill=cut, colour=cut)) +
  scale_colour_manual(values=c("Fair"="brown",
                             "Good"="blue",
                             "Very Good"="green",
                             "Premium"="red",
                             "Ideal"="yellow")) +
  scale_fill_manual(values=c("Fair"="yellow",
                             "Good"="light green",
                             "Very Good"="light blue",
                             "Premium"="pink",
                             "Ideal"="purple"))

enter image description here