R语言 使用stat_pvalue_manual将p值添加到ggplot时出错

rn0zuynd  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(126)

我有一个关于stat_pvalue_manual的问题。每当我使用stat_pvalue_manual函数运行它时,它们都会显示错误。我尝试使用stat_compare_means,但它与p.adj value不匹配,所以我需要返回stat_pvalue_manual。下面是我的脚本和数据。我希望有人能帮助我解决这个问题。非常感谢您!

# Create vectors for each variable
medium <- c(
  "MM", "MM", "MM", "MM", "MM", "MM",
  "MM", "MM", "MM", "MM", "MM",
  "MM", "MM", "MM", "MM", "MM",
  "MMGA", "MMGA", "MMGA", "MMGA", "MMGA", "MMGA",
  "MMGA", "MMGA", "MMGA", "MMGA", "MMGA"
)

strains <- c(
  "A", "A", "A", "A", "A", "A",
  "B", "B", "B", "B", "B",
  "C", "C", "C", "C", "C",
  "A", "A", "A", "A", "A", "A",
  "B", "B", "B", "B", "B"
)

value <- c(
  0.642396224, 0.642973791, 0.560378425, 0.844780865, 1.202418689, 2.107052006,
  0.286002062, 0.207255769, 0.121489854, 0.149735456, 0.292497354,
  0.188256332, 0.181433285, 0.407696852, 0.37862504, 0.891960999, 0.465748762,
  1.003103112, 1.000750442, 1.001402757, 1.006722153, 0.996919936, 0.991101601,
  0.997623183, 0.994042211, 0.999101239, 0.996893329, 1.000764356
)

# Create a dataframe
ggdat <- data.frame(medium, strains, value)

错误:!计算美观性时出错。第4层出错。由FUN()中的错误引起:!object 'strains' not found运行rlang::last_trace()查看错误发生的位置。警告消息:名称标准化后的重复美学:大小

#####################
library(ggprism)
library(readxl)
library(ggplot2)
library(RColorBrewer)
library(ggpubr)
library(rstatix)

ggdat <- read.csv("ggdat_demo.csv")

#calculate p-val
stat.test <- ggdat %>%
  group_by(medium) %>%
  t_test(value ~ strains) %>%
  adjust_pvalue(method = "bonferroni") %>%
  add_significance() %>%   
  add_xy_position(x = "strains", dodge = 0.8)

#data for mean and sd
df.summary <- ggdat %>%  group_by(strains, medium) %>%
  summarise(sd = sd(value),    value = mean(value)) # generate mean and sd for col plot

ggdat$strains <- factor(ggdat$strains, levels = c("A","B", "C", "D"))
df.summary$strains <- factor(df.summary$strains, levels = c("A","B", "C", "D"))

#graph eror when I add p value
ggplot(ggdat, aes(strains, value, fill=strains)) +
  facet_grid(. ~ medium) + 
  geom_col(data = df.summary, position = position_dodge(0.8), width = 0.7, alpha=1) +
  geom_errorbar(data = df.summary, aes(ymin = value-sd, ymax = value+sd), 
                color="#282828",width = 0.4, position = position_dodge(0.8)) + 
  geom_point(size=2, position = position_jitterdodge(jitter.width = .5, dodge.width = .8),
             cex = 3, alpha=.8,  color="#282828", shape=21, stroke = .75) +
 # add_pvalue(stat.test, label = "p.adj") +  
  stat_pvalue_manual(stat.test,  label = "p.adj", tip.length = 0) +
  theme(legend.text = element_text(size = 12), legend.title = element_text(size = 12),
        legend.position = "top",
        axis.title.y = element_text(face = "plain", size = 12, color = "black", margin = margin(0,0,0,0,"cm")),
        axis.title.x = element_text(face = "plain", size = 12, color = "black", margin = margin(0,0,0,0,"cm")),
        axis.text.x = element_text(face = "plain", size = 12, color = "black", angle = 35, vjust = 1, hjust = 1, margin = margin(0,0,0.2,0,"cm")),#
        axis.text.y = element_text(face = "plain", size = 12, color = "black", angle = 0, margin = margin(0,0,0,0.2,"cm"))) +
  scale_y_continuous(limits = c(0,3), breaks = c(0, 0.5,1, 1.5, 2, 2.5))+
  scale_color_manual(name= "Strains",values = c("#E6A226", "#BABBBB", "#21BDC2", "#7AD1ED"),labels=c("A","B", "C", "D")) +
  scale_fill_manual(name= "Strains",values = c("#E6A226", "#BABBBB", "#21BDC2", "#7AD1ED"), labels=c("A","B", "C", "D")) +
  guides(fill=guide_legend(nrow=1,byrow=TRUE),  color=guide_legend(nrow=1, byrow=TRUE))+
  theme_bw()
vybvopom

vybvopom1#

有几个问题需要解决,但最大的问题(据我所知)是将“global”aes()用于不同的数据源。如果为每个几何体分别指定美学,则会得到输出,例如:

library(tidyverse)
library(RColorBrewer)
library(ggpubr)
library(rstatix)
#> 
#> Attaching package: 'rstatix'
#> The following object is masked from 'package:stats':
#> 
#>     filter

# Create vectors for each variable
medium <- c(
  "MM", "MM", "MM", "MM", "MM", "MM",
  "MM", "MM", "MM", "MM", "MM",
  "MM", "MM", "MM", "MM", "MM",
  "MMGA", "MMGA", "MMGA", "MMGA", "MMGA", "MMGA",
  "MMGA", "MMGA", "MMGA", "MMGA", "MMGA"
)

strains <- c(
  "A", "A", "A", "A", "A", "A",
  "B", "B", "B", "B", "B",
  "C", "C", "C", "C", "C",
  "A", "A", "A", "A", "A", "A",
  "B", "B", "B", "B", "B"
)

value <- c(
  0.642396224, 0.642973791, 0.560378425, 0.844780865, 1.202418689, 2.107052006,
  0.286002062, 0.207255769, 0.121489854, 0.149735456, 0.292497354,
  0.188256332, 0.181433285, 0.407696852, 0.37862504, 0.891960999, 0.465748762,
  1.003103112, 1.000750442, 1.001402757, 1.006722153, 0.996919936, 0.991101601,
  0.997623183, 0.994042211, 0.999101239, 0.996893329
)

# Create a dataframe
ggdat <- data.frame(medium, strains, value)

#calculate p-val
stat.test <- ggdat %>%
  group_by(medium) %>%
  t_test(value ~ strains) %>%
  adjust_pvalue(method = "bonferroni") %>%
  add_significance() %>%   
  add_xy_position(x = "strains", dodge = 0.8)

#data for mean and sd
df.summary <- ggdat %>%  group_by(strains, medium) %>%
  summarise(sd = sd(value),    value = mean(value)) # generate mean and sd for col plot
#> `summarise()` has grouped output by 'strains'. You can override using the
#> `.groups` argument.

ggdat$strains <- factor(ggdat$strains, levels = c("A","B", "C", "D"))
df.summary$strains <- factor(df.summary$strains, levels = c("A","B", "C", "D"))
df.summary <- ungroup(df.summary)

#graph eror when I add p value
ggplot(ggdat) +
  facet_grid(. ~ medium) + 
  geom_col(data = df.summary, aes(strains, value, fill=strains),
           position = position_dodge(0.8), width = 0.7, alpha=1) +
   geom_errorbar(data = df.summary, aes(x = strains,
                                        y = value,
                                        ymin = value-sd, ymax = value+sd), 
                 color="#282828",width = 0.4, position = position_dodge(0.8)) + 
  geom_point(aes(strains, value, fill=strains),
             size=2, position = position_jitterdodge(jitter.width = .5, dodge.width = .8),
             cex = 3, alpha=.8,  color="#282828", shape=21, stroke = .75) +
  # add_pvalue(stat.test, label = "p.adj") +  
  stat_pvalue_manual(stat.test, label = "p.adj", tip.length = 0) +
  theme(legend.text = element_text(size = 12), legend.title = element_text(size = 12),
        legend.position = "top",
        axis.title.y = element_text(face = "plain", size = 12, color = "black", margin = margin(0,0,0,0,"cm")),
        axis.title.x = element_text(face = "plain", size = 12, color = "black", margin = margin(0,0,0,0,"cm")),
        axis.text.x = element_text(face = "plain", size = 12, color = "black", angle = 35, vjust = 1, hjust = 1, margin = margin(0,0,0.2,0,"cm")),#
        axis.text.y = element_text(face = "plain", size = 12, color = "black", angle = 0, margin = margin(0,0,0,0.2,"cm"))) +
  scale_y_continuous(limits = c(0,3), breaks = c(0, 0.5,1, 1.5, 2, 2.5))+
  scale_color_manual(name= "Strains",values = c("#E6A226", "#BABBBB", "#21BDC2", "#7AD1ED"),labels=c("A","B", "C", "D")) +
  scale_fill_manual(name= "Strains",values = c("#E6A226", "#BABBBB", "#21BDC2", "#7AD1ED"), labels=c("A","B", "C", "D")) +
  guides(fill=guide_legend(nrow=1,byrow=TRUE),  color=guide_legend(nrow=1, byrow=TRUE))+
  theme_bw()
#> Warning: Duplicated aesthetics after name standardisation: size

创建于2023-09-21使用reprex v2.0.2
这是你期望的输出吗?这能解决你的问题吗?

相关问题