简体   繁体   English

ggplot2:如何在箱线图中调整填充颜色(并更改图例文本)?

[英]ggplot2: How to adjust fill colour in a boxplot (and change legend text)?

How can I: 我怎样才能:

  1. Set the fill colors in the boxplot below? 在下面的箱子图中设置填充颜色? I tried the argument "colour" but that failed. 我尝试了“颜色”这个论点,但那次失败了。
  2. Change the legend text from "0", "1" to something else? 将图例文本从“0”,“1”更改为其他内容?

     require(ggplot2) ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot(aes(fill=factor(vs), colour=c("grey50", "white"))) 

Instead of the colour aesthetic, you want to adjust the fill aesthetic. 您需要调整填充美感,而不是颜色美感。 You can handle both of your questions (and much more) by adjusting the scale: 您可以通过调整比例来处理您的两个问题(以及更多):

ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(vs))) + 
  geom_boxplot() +
  scale_fill_manual(name = "This is my title", values = c("pink", "green")
                    , labels = c("0" = "Foo", "1" = "Bar"))

The ggplot2 website help page for scale_manual is full of good examples. 对于GGPLOT2网站帮助页面scale_manual是满不错的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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