ggplot2: geom_bar stacked barplot, specify bar out

2020-07-22 19:04发布

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.

1条回答
Explosion°爆炸
2楼-- · 2020-07-22 19:41

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

查看更多
登录 后发表回答