简体   繁体   中英

ggplot2: geom_bar stacked barplot, specify bar outline color

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 (eg 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.

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"))

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM