如何使用ggplot更改箱形图的轮廓颜色? [英] How do I change the outline colours of a boxplot with ggplot?

查看:1522
本文介绍了如何使用ggplot更改箱形图的轮廓颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据具有相同的值,因此它们像箱形图中的一条线一样出现。但是,这意味着我无法说出组之间的区别,因为填充不会显示出来。如何将箱形图的轮廓更改为特定颜色。

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"))+


推荐答案

您必须:


  1. 为美学添加颜色=清晰度

  2. scale_color_manual 添加到具有所需颜色的ggplot对象中

  3. 名称 scale_color_manual 相同 scale_fill_manual 的方式来获取单个组合图例

  1. Add color = clarity to aesthetics
  2. Add scale_color_manual to ggplot object with wanted colors
  3. Name scale_color_manual the same way as scale_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更改箱形图的轮廓颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆