首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在geom_boxplot p-值前面添加ggplot2中的"p =“

在geom_boxplot p-值前面添加ggplot2中的"p =“
EN

Stack Overflow用户
提问于 2022-09-06 20:41:14
回答 1查看 50关注 0票数 0

P-值可以使用函数ggplot2添加到ggpubr::stat_compare_mean()图中。但是,我不能让文本"p =“出现在p-值前面。在函数的帮助页面中,如何在p-值前面添加"p =“的例子有一些,但是它们似乎不起作用。

示例

代码语言:javascript
复制
library(ggplot2)
library(ggpubr)
library(dplyr)

data("Cars93")

# List of the comparisons I would like to make for which p-values will be derived

my_comparisons <- list(c("Front", "Rear"),
                       c("Front", "4WD"),
                       c("Rear", "4WD"))

# creates the figure with p-value but no label indicating the values are p-values

Cars93 %>%
  mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
  ggplot(aes(x = DriveTrain, y = Price)) +
  stat_compare_means(paired = F,
                     comparisons = my_comparisons) +
  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) +
  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())

遵循?stat_compare_means页面底部的示例,建议使用不起作用的aes(label = paste0("p = ", ..p.format..)

代码语言:javascript
复制
?stat_compare_means

Cars93 %>%
  mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
  ggplot(aes(x = DriveTrain, y = Price)) +
  stat_compare_means(paired = F,
                     comparisons = my_comparisons,
                     aes(label = paste0("p = ", ..p.format..))) +
  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) +
  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())

如果您查看label帮助页面上的?stat_compare_means参数,它说允许的值包括"p.signif""p.format",这使我认为..p.format..是不推荐的,所以我尝试在"p.format"中添加也不起作用的值。

代码语言:javascript
复制
Cars93 %>%
  mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
  ggplot(aes(x = DriveTrain, y = Price)) +
  stat_compare_means(paired = F,
                     comparisons = my_comparisons,
                     aes(label = paste0("p = ", "p.format"))) +
  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) +
  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())

最后,我希望在p-值之前加上p =,这样标签就可以说p= 0.00031,p= 0.059,p= 0.027。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-06 21:20:33

当您使用比较列表时,stat_compare_means默认使用来自ggsignif包的geom_signif,实质上是一个美化的包装函数。这样做,您就失去了一些格式化灵活性。在这种情况下,最好直接使用geom_signif

代码语言:javascript
复制
library(ggsignif)

Cars93 %>%
  mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
  ggplot(aes(x = DriveTrain, y = Price)) +
  geom_signif(y_position = c(55, 60, 65),
              comparisons = my_comparisons,
              map_signif_level = function(x) paste("p =", scales::pvalue(x))) +
  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) +
  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())

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73627556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档