如何使用 ggplot 更改箱线图的轮廓颜色?

How do I change the outline colours of a boxplot with ggplot?

我的数据具有完全相同的值,因此它们只是箱形图中的一条线。然而,这意味着我无法区分组之间的差异,因为填充没有显示出来。如何将箱线图的轮廓更改为特定颜色。

注意:我不希望所有的轮廓颜色都是相同的颜色,如下一行代码:

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 的所有图(填充为 grey40)用黑色勾勒出轮廓,而 SI2 的图(用浅天蓝填充)用蓝色勾勒出轮廓。

以下似乎不起作用

geom_boxplot(colour = c("black","blue"))+

scale_color_identity(c("black", "blue"))+

scale_color_manual(values = c("black", "blue"))+

你必须:

  1. 为美学添加color = clarity
  2. scale_color_manual 添加到具有所需颜色的 ggplot 对象
  3. 以与 scale_fill_manual 相同的方式命名 scale_color_manual 以获得单个组合图例

代码:

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)

剧情: