问题描述
我的数据具有相同的值,因此它们像箱形图中的一条线一样出现。但是,这意味着我无法说出组之间的区别,因为填充不会显示出来。如何将箱形图的轮廓更改为特定颜色。
My data has values which are all the same so they are coming up as just a line in a box plot. This, however, means that I can't tell the difference between groups as the fill doesn't show up. How can I change the outlines of the boxplot to be a specific colour.
注意:我不希望所有轮廓线颜色都相同,如下一行代码所示:
Note: I do not want all the outline colours to be the same colour, as in the next line of code:
library(dplyr)
library(ggplot2)
diamonds %>%
filter(clarity %in% c("I1","SI2")) %>%
ggplot(aes(x= color, y= price, fill = clarity))+
geom_boxplot(colour = "blue")+
scale_fill_manual(name= "Clarity", values = c("grey40", "lightskyblue"))+
facet_wrap(~cut)
相反,我希望I1的所有图(用灰色40填充)都用黑色绘制轮廓,而SI2的图(用浅天蓝色填充)用蓝色绘制轮廓。
Instead, I would like all the plots for I1 (filled with grey40) to be outlined in black while the plots for SI2 (filled with lightskyblue) to be outlined in blue.
以下内容似乎无效
geom_boxplot(colour = c("black","blue"))+
OR
scale_color_identity(c("black", "blue"))+
OR
scale_color_manual(values = c("black", "blue"))+
推荐答案
您必须:
- 为美学添加
颜色=清晰度
- 将
scale_color_manual
添加到具有所需颜色的ggplot对象中 - 名称
scale_color_manual
相同scale_fill_manual
的方式来获取单个组合图例
- Add
color = clarity
to aesthetics - Add
scale_color_manual
to ggplot object with wanted colors - Name
scale_color_manual
the same way asscale_fill_manual
to get single combined legend
代码:
library(dplyr)
library(ggplot2)
diamonds %>%
filter(clarity %in% c("I1","SI2")) %>%
ggplot(aes(x= color, y= price, fill = clarity, color = clarity))+
geom_boxplot()+
scale_fill_manual(name= "Clarity", values = c("grey40", "lightskyblue"))+
scale_color_manual(name = "Clarity", values = c("black", "blue"))+
facet_wrap( ~ cut)
图:
这篇关于如何使用ggplot更改箱形图的轮廓颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!