代码之家  ›  专栏  ›  技术社区  ›  tassones

如何在指数(log10)y轴上的方框图上方显示p值?

  •  1
  • tassones  · 技术社区  · 4 年前

    我有一个包含三个组(组1、组2、组3)的数据帧。我想在中显示它们的平均值比较的p值 ggplot2 然而,我可以做到的是,这些值是堆叠在一起的,很难看到正在进行比较的内容。当我尝试使用 y_position() 函数,箱图折叠(我认为是因为y轴是log10),但p值不再堆叠在一起。如何防止箱线图塌陷,并保持p值的显示,以便您可以看到正在进行的比较?

    示例数据

    library(ggplot2)
    library(dplyr)
    library(ggsignif)
    
    df <- data.frame(matrix(ncol = 2, nrow = 30))
    colnames(df)[1:2] <- c("group", "value")
    df$group <- rep(c("group1","group2","group3"), each = 10)
    df[1:10,2] <- rexp(10, 1/10)
    df[11:20,2] <- rexp(10, 1/100)
    df[21:30,2] <- rexp(10, 1/900)
    
    # Need to say what should be compared for p-value determination
    
    my_comparisons <- list(c("group1", "group2"),
                           c("group1", "group3"),
                           c("group2", "group3"))
    

    显示分布的方框图 value 然而,对于每个组,p值是彼此的上下,因此无法在组之间进行比较。

    df %>%
      mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
      ggplot(aes(x = group, y = value)) +
      geom_signif(comparisons = my_comparisons,
                  map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
      scale_y_log10() +
      geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0) +
      geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2) +
      labs(x = "",
           y = "value") +
      theme_bw() +
      theme(axis.text.x = element_text(size = 16, color = "black"),
            axis.text.y = element_text(size = 16, color = "black"),
            axis.title = element_text(size = 16, color = "black"),
            axis.title.x = element_text(vjust = -0.5),
            panel.grid = element_blank(),
            panel.background = element_blank())
    

    调整 y_position() p值应该显示在哪里,但这会折叠y轴。我在中尝试了几个值 y_position .

    df %>%
      mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
      ggplot(aes(x = group, y = value)) +
      geom_signif(y_position = c(2000,1800,1600),
                  comparisons = my_comparisons,
                  map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
      scale_y_log10() +
      geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0) +
      geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2) +
      labs(x = "",
           y = "value") +
      theme_bw() +
      theme(axis.text.x = element_text(size = 16, color = "black"),
            axis.text.y = element_text(size = 16, color = "black"),
            axis.title = element_text(size = 16, color = "black"),
            axis.title.x = element_text(vjust = -0.5),
            panel.grid = element_blank(),
            panel.background = element_blank())
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   Allan Cameron    4 年前

    由于某些原因,此参数会忽略轴变换。因此,您需要使用所需位置的log10值:

    
    df %>%
      mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
      ggplot(aes(x = group, y = value)) +
      geom_signif(comparisons = my_comparisons,
                  y_position = log10(c(5000, 10000, 25000)),
                  map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
      scale_y_log10() +
      geom_boxplot(outlier.colour="white", outlier.fill = "white", 
                   -outlier.shape = 1, outlier.size = 0) +
      geom_jitter(shape=1, position=position_jitter(0.2), color = "black", 
                  fill = "white", size = 2) +
      labs(x = "",
           y = "value") +
      theme_bw() +
      theme(axis.text.x = element_text(size = 16, color = "black"),
            axis.text.y = element_text(size = 16, color = "black"),
            axis.title = element_text(size = 16, color = "black"),
            axis.title.x = element_text(vjust = -0.5),
            panel.grid = element_blank(),
            panel.background = element_blank())
    

    enter image description here

    推荐文章