ggplot2:geom_bar堆叠的条形图,指定条形轮廓颜色 [英] ggplot2: geom_bar stacked barplot, specify bar outline color

查看:836
本文介绍了ggplot2:geom_bar堆叠的条形图,指定条形轮廓颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在ggplot2中的堆叠条形图上指定轮廓颜色.在下面的代码中,我指定color="green",它为每个条形图提供绿色轮廓.我想为每个条指定不同的轮廓颜色(例如cut=Fair将用黄色填充并用橙色轮廓,cut=Good将用浅绿色填充并用深绿色轮廓等).

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

我尝试了scale_color_manual()并在geom_bar()美学中指定了颜色向量,但都没有奏效.

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

推荐答案

您必须将两种美感都映射到cut变量,然后才能使用scale_colour_manual.这是一个(丑陋的)示例:

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

这篇关于ggplot2:geom_bar堆叠的条形图,指定条形轮廓颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆