如何将比较条添加到绘图中以表示p值对应的比较

时间:2017-09-27 22:09:37

标签: r ggplot2 comparison ggpubr

我正在使用以下数据框:

df1 <- structure(list(Genotype = structure(c(1L, 1L, 1L, 1L, 1L,
2L,2L,2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L,1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L),
.Label= c("miR-15/16 FL", "miR-15/16 cKO"), class = "factor"), 
Tissue = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L), .Label = c("iLN", "Spleen", "Skin", "Colon"), class = "factor"), 
`Cells/SC/Live/CD8—,, CD4+/Foxp3+,Median,<BV421-A>,CD127` = c(518L, 
715L, 572L, 599L, 614L, 881L, 743L, 722L, 779L, 843L, 494L, 
610L, 613L, 624L, 631L, 925L, 880L, 932L, 876L, 926L, 1786L, 
2079L, 2199L, 2345L, 2360L, 2408L, 2509L, 3129L, 3263L, 3714L, 
917L, NA, 1066L, 1059L, 939L, 1269L, 1047L, 974L, 1048L, 
1084L)),
.Names = c("Genotype", "Tissue", "Cells/SC/Live/CD8—,,CD4+/Foxp3+,Median,<BV421-A>,CD127"),
row.names = c(NA, -40L), class = c("tbl_df", "tbl", "data.frame"))

并尝试使用ggplot2绘制图,其中框图和点显示按“组织”分组并由“基因型”交错。显着性值正在显示,但我想添加线来表示正在进行的比较,让它们从每个“miR-15/16 FL”盒子图的中心开始,并在每个“miR-15 /”的中心结束16 cKO“框图并直接位于显着性值下方。下面是我用来生成图表的代码:

library(ggplot2)
library(ggpubr)
color.groups <- c("black","red")
names(color.groups) <- unique(df1$Genotype)
shape.groups <- c(16, 1)
names(shape.groups) <- unique(df1$Genotype)

ggplot(df1, aes(x = Tissue, y = df1[3], color = Genotype, shape = Genotype)) +
  geom_boxplot(position = position_dodge(), outlier.shape = NA) +
  geom_point(position=position_dodge(width=0.75)) +
  ylim(0,1.2*max(df1[3], na.rm = TRUE)) +
  ylab('MFI CD127 (of CD4+ Foxp3+ T cells') +
  scale_color_manual(values=color.groups) +
  scale_shape_manual(values=shape.groups) +
  theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                     panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
                     axis.title.x=element_blank(), aspect.ratio = 1,
                     text = element_text(size = 9)) +
  stat_compare_means(show.legend = FALSE, label = 'p.format', method = 't.test',
                     label.y = c(0.1*max(df1[3], na.rm = TRUE) + max(df1[3][c(1:10),], na.rm = TRUE),
                                 0.1*max(df1[3], na.rm = TRUE) + max(df1[3][c(11:20),], na.rm = TRUE),
                                 0.1*max(df1[3], na.rm = TRUE) + max(df1[3][c(21:30),], na.rm = TRUE),
                                 0.1*max(df1[3], na.rm = TRUE) + max(df1[3][c(31:40),], na.rm = TRUE)))

enter image description here

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

我创建了三次调用geom_segment的括号。这些调用使用创建的新dmax数据框来提供用于定位括号和p值标签的参考y值。值er用于调整这些位置。

我对您的代码进行了一些其他更改。

  1. 将第三列的名称更改为temp,并在调用ggplot时使用此名称y=temp。您的原始代码使用y=df1[3],它基本上在绘图环境之外到达父环境中的df1对象,这可能会导致问题。此外,使用简短的名称可以更容易地生成dmax数据框并引用其列。

  2. dmax数据框用于label.y中的stat_compare_means位置,这样可以减少所需的代码量。 (很明显,stat_compare_means似乎需要硬编码label.y位置,而不是从aes数据映射中获取它们。)

  3. 将p值标签放置在每对箱形图上方的绝对距离(使用值e),而不是乘法距离。这样可以更容易地保持p值标签,括号和箱形图之间的间距一致。

  4. # Use a short column name for the third column
    names(df1)[3] = "temp"
    
    # Generate data frame of reference y-values for p-value labels and bracket positions
    dmax = df1 %>% group_by(Tissue) %>% 
      summarise(temp=max(temp, na.rm=TRUE),
                Genotype=NA)
    
    # For tweaking position of brackets
    e = 350
    r = 0.6
    w = 0.19
    bcol = "grey30"
    
    ggplot(df1, aes(x = Tissue, y = temp, color = Genotype, shape = Genotype)) +
      geom_boxplot(position = position_dodge(), outlier.shape = NA) +
      geom_point(position=position_dodge(width=0.75)) +
      ylim(0,1.2*max(df1[3], na.rm = TRUE)) +
      ylab('MFI CD127 (of CD4+ Foxp3+ T cells') +
      scale_color_manual(values=color.groups) +
      scale_shape_manual(values=shape.groups) +
      theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                         panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
                         axis.title.x=element_blank(), aspect.ratio = 1,
                         text = element_text(size = 9)) +
      stat_compare_means(show.legend = FALSE, label = 'p.format', method = 't.test',
                         label.y = e + dmax$temp) +
      geom_segment(data=dmax,
                   aes(x=as.numeric(Tissue)-w, xend=as.numeric(Tissue)+w, 
                       y=temp + r*e, yend=temp + r*e), size=0.3, color=bcol, inherit.aes=FALSE) +
      geom_segment(data=dmax,
                   aes(x=as.numeric(Tissue) + w, xend=as.numeric(Tissue) + w, 
                       y=temp + r*e, yend=temp + r*e - 60), size=0.3, color=bcol, inherit.aes=FALSE) +
      geom_segment(data=dmax,
                   aes(x=as.numeric(Tissue) - w, xend=as.numeric(Tissue) - w, 
                       y=temp + r*e, yend=temp + r*e - 60), size=0.3, color=bcol, inherit.aes=FALSE)
    

    enter image description here

    为了解决您的评论,以下是一个示例,表明上述方法固有地适应任意数量的x类别。

    让我们首先添加两个新的组织类别:

    library(forcats)
    
    df1$Tissue = fct_expand(df1$Tissue, "Tissue 5", "Tissue 6")
    df1$Tissue[seq(1,20,4)] = "Tissue 5"
    df1$Tissue[seq(21,40,4)] = "Tissue 6"
    
    dmax = df1 %>% group_by(Tissue) %>% 
      summarise(temp=max(temp, na.rm=TRUE),
                Genotype=NA)
    

    现在运行与上面列出的完全相同的绘图代码以获得以下图:

    enter image description here