ggplot2:如何调整boxplot中的填充颜色(和更改图例文本)?
问题描述:
我如何能:
How can I:
- 在下面的boxplot中设置填充颜色?
-
将图例文本从0,1更改为其他内容?
- Set the fill colors in the boxplot below? I tried the argument "colour" but that failed.
Change the legend text from "0", "1" to something else?
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"))
scale_manual 的ggplot2网站帮助页面充满了很好的例子。
The ggplot2 website help page for scale_manual is full of good examples.