首页 文章

如何使用ggplot更改boxplot的轮廓颜色?

提问于
浏览
1

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

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

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(用lightskyblue填充)的图用蓝色勾勒出来 .

以下似乎不起作用

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

要么

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

要么

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

1 回答

  • 1

    你必须:

    • color = clarity 添加到美学中

    • scale_color_manual 添加到想要颜色的ggplot对象

    • 名称 scale_color_manualscale_fill_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)
    

    情节:

    enter image description here

相关问题