【问题标题】:How to create box plots with all points where for each group, the color of the points can be assigned manually如何创建包含所有点的箱线图,对于每个组,可以手动分配点的颜色
【发布时间】:2017-09-25 17:15:02
【问题描述】:

我有一个数据框:

> dput(df2)
structure(list(Genotype = c("miR-15/16 FL", "miR-15/16 FL", "miR-15/16 FL", 
"miR-15/16 FL", "miR-15/16 FL", "miR-15/16 cKO", "miR-15/16 cKO", 
"miR-15/16 cKO", "miR-15/16 cKO", "miR-15/16 cKO"),
`Cells/SC/Live/CD8—,, CD4+/Foxp3-,Median,<BV421-A>,CD127` = c(1191L, 1325L, 1089L, 1154L, 1147L, 1735L, 1441L, 1455L, 1560L, 1623L)),
.Names = c("Genotype", "Cells/SC/Live/CD8—,, CD4+/Foxp3-,Median,<BV421-A>,CD127"),
row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
MFI=c(1191,1325,1089,1154,1147,1735,1441,1455,1560,1623))

我想制作一个箱线图,为每个组(miR-15/16 FL 和 miR-15/16 cKO)绘制每个点,并拥有所有的

'miR-15/16 FL' 

点被封闭的黑色圆圈和所有的

'miR-15/16 cKO' 

点是开放的红色圆圈。我希望能够手动调整每组点的颜色和形状/大小。

到目前为止,我已经尝试过:

library(ggplot2)
ggplot(data=df2, aes(x = df2$Genotype, y = df2[2])) +
geom_boxplot(outlier.shape = NA) +
geom_jitter(position = position_jitter(width = .2), shape=1, size=5) +
ylim(0,max(df2[2])+10)

但我无法弄清楚如何独立调整颜色/形状

'miR-15/16 FL'

'miR-15/16 cKO'

感谢您对此的任何帮助!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这可能会让你开始:

    ggplot(data=df2, aes(x = Genotype, y = MFI)) +
      geom_boxplot(outlier.shape = NA) +
      geom_jitter(aes(col = Genotype, shape = Genotype),position = position_jitter(width = .2), size=5) +
      ylim(0,max(df2$MFI)+10)+
      scale_shape_manual(values = c(1,16))+
      scale_color_manual(values = c('red', 'black'))
    

    我发现这个网站非常有用: http://sape.inf.usi.ch/quick-reference/ggplot2/shape

    【讨论】:

      【解决方案2】:

      @Balter 击败了我……唯一的区别是我在 ggplot 调用之外设置了颜色和形状参数,以便将来进行程序访问。

      library(ggplot2)
      
      df2 <- data.frame(Genotype = c('WT','WT','WT','WT','WT',
                                     'cKO','cKO','cKO','cKO','cKO'),
                        MFI=c(1191,1325,1089,1154,1147,1735,1441,1455,1560,1623))
      
      color.groups <- c(WT="black", cKO="red")
      shape.groups <- c(WT=20, cKO=21)
      
      ggplot(data=df2, aes(x = df2$Genotype, y = df2$MFI)) +
        geom_boxplot(outlier.shape = NA) +
        geom_point(position = position_jitter(width = .2), size=5, 
                   aes(color=Genotype, shape = Genotype)) +
        ylim(0,max(df2$MFI)+10) + 
        scale_color_manual(values=color.groups) +
        scale_shape_manual(values=shape.groups)
      

      更新:

      library(ggplot2)
      
      df2 <- data.frame(Genotype = c('miR-15/16 WT','miR-15/16 WT','miR-15/16 WT','miR-15/16 WT','miR-15/16 WT',
                                     'miR-15/16 cKO','miR-15/16 cKO','miR-15/16 cKO','miR-15/16 cKO','miR-15/16 cKO'),
                        MFI=c(1191,1325,1089,1154,1147,1735,1441,1455,1560,1623))
      
      color.groups <- c(`miR-15/16 WT`="black", `miR-15/16 cKO`="red")
      shape.groups <- c(`miR-15/16 WT`=20, `miR-15/16 cKO`=21)
      
      ggplot(data=df2, aes(x = Genotype, y = MFI)) +
        geom_boxplot(outlier.shape = NA) +
        geom_point(position = position_jitter(width = .2), size=5, 
                   aes(color=Genotype, shape = Genotype)) +
        ylim(0,max(df2$MFI)+10) + 
        scale_color_manual(values=color.groups) +
        scale_shape_manual(values=shape.groups)
      

      更新2:

      df2 <- structure(list(Genotype = c("miR-15/16 FL", "miR-15/16 FL", "miR-15/16 FL", 
                                         "miR-15/16 FL", "miR-15/16 FL", "miR-15/16 cKO", "miR-15/16 cKO", 
                                         "miR-15/16 cKO", "miR-15/16 cKO", "miR-15/16 cKO"),
                            `Cells/SC/Live/CD8—,, CD4+/Foxp3-,Median,<BV421-A>,CD127` = c(1191L, 1325L, 1089L, 1154L, 1147L, 1735L, 1441L, 1455L, 1560L, 1623L)),
                       .Names = c("Genotype", "Cells/SC/Live/CD8—,, CD4+/Foxp3-,Median,<BV421-A>,CD127"),
                       row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
      colnames(df2) <- c("Genotype", "MFI")
      
      color.groups <- c("black","red")
      names(color.groups) <- unique(df2$Genotype)
      shape.groups <- c(20, 21)
      names(shape.groups) <- unique(df2$Genotype)
      
      ggplot(data=df2, aes(x = Genotype, y = MFI)) +
        geom_boxplot(outlier.shape = NA) +
        geom_point(position = position_jitter(width = .2), size=5, 
                   aes(color=Genotype, shape = Genotype)) +
        ylim(0,max(df2$MFI)+10) + 
        scale_color_manual(values=color.groups) +
        scale_shape_manual(values=shape.groups)
      

      【讨论】:

      • 是的,这是个好建议。如果您正在制作一堆图并且正在修补颜色和形状,这很有用。为您节省大量调整每个图的时间,因为您只需调整变量并将效果一举传播到所有图。更不用说,更容易知道您正在调整哪种颜色。
      • 酷,我喜欢在 ggplot 调用之外设置颜色和形状参数的想法。当我使用我正在使用的实际基因型标签时,我注意到奇怪的行为:('miR-15/16 WT'和'miR-15/16 cKO')。我尝试将 color.groups
      • 请看我的更新。您需要将“mir-15/16 cKO”放在反引号“`”中
      • @emilliman5 我的似乎与命名向量中的引号一起使用。反引号也有效。可能是 ggplot2 还是 R 版本?
      • @JohnGagnon,请使用您正在使用的代码和dput 更新您的原始帖子以获取数据
      猜你喜欢
      • 2020-09-28
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      相关资源
      最近更新 更多