尝试将 pvalues 手动添加到 ggplot R 中的箱线图后出现错误
got an error after trying to add pvalues manually to a boxplot in ggplot R
我正在尝试将在其他地方计算的 p 值添加到我的箱线图中。在尝试使用函数添加 p 值之前,箱线图工作得很好:stat_pvalue_manual。我的代码如下:
df <- data.frame(
method = c(rep('JXD', 100), rep('ILL', 100),rep('NP', 100) ),
value = c((runif(100, min=400, max=800)), runif(100, min=500, max=850), runif(100, min=900, max=1500))
)
ggplot(df, aes(method, value, fill = method)) + # This is the plot function
geom_violin() +
geom_boxplot(width=0.2, fill="white", alpha = 0.1) +
labs(x = "Method", fill="Method")
在此之后,我尝试从其他程序添加 p 值:
stat.test <- tibble::tribble(
~group1, ~group2, ~p.adj, ~p.signif,
"ILL", "JXD", 6.466374e-01, 'n.s',
"ILL", "NP", 5.301167e-50, '****'
)
ggplot(df, aes(method, value, fill = method)) + # This is the plot function
geom_violin() +
geom_boxplot(width=0.2, fill="white", alpha = 0.1) +
labs(x = "Method", fill="Method") +
stat_pvalue_manual(
stat.test,
y.position = 900, step.increase = 1,
label = "p.adj"
)
但出现以下错误:
Error in FUN(X[[i]], ...) : object 'method' not found
我尝试改用 ggboxplot 函数,通过在引号 'method' 之间加上它可以正常工作,这不适用于 ggplot 函数。但是使用前者我无法得到我想要的数字。
g <- ggboxplot(df, x = "method", y = "value", width = 0.8)
g+ stat_pvalue_manual(
stat.test,
y.position = 900, step.increase = 0.7,
label = "p.signif"
我不明白哪里错了。
非常感谢!
问题是您将 fill=method
指定为全球美学。因此,在 stat_pvalue_manual
中也在您的数据框 stat.test
中寻找列名 method
。
要解决此问题,请将 fill=method
设为 geom_violin
的本地美学:
library(ggplot2)
library(ggpubr)
df <- data.frame(
method = c(rep("JXD", 100), rep("ILL", 100)),
value = c((runif(100, min = 400, max = 800)), runif(100, min = 500, max = 850))
)
stat.test <- tibble::tribble(
~group1, ~group2, ~p.adj, ~p.signif,
"ILL", "JXD", 6.466374e-01, "n.s",
"ILL", "NP", 5.301167e-50, "****"
)
ggplot(df, aes(method, value)) + # This is the plot function
geom_violin(aes(fill = method)) +
geom_boxplot(width = 0.2, fill = "white", alpha = 0.1) +
labs(x = "Method", fill = "Method") +
stat_pvalue_manual(
stat.test,
y.position = 900, step.increase = 1,
label = "p.adj"
)
我正在尝试将在其他地方计算的 p 值添加到我的箱线图中。在尝试使用函数添加 p 值之前,箱线图工作得很好:stat_pvalue_manual。我的代码如下:
df <- data.frame(
method = c(rep('JXD', 100), rep('ILL', 100),rep('NP', 100) ),
value = c((runif(100, min=400, max=800)), runif(100, min=500, max=850), runif(100, min=900, max=1500))
)
ggplot(df, aes(method, value, fill = method)) + # This is the plot function
geom_violin() +
geom_boxplot(width=0.2, fill="white", alpha = 0.1) +
labs(x = "Method", fill="Method")
在此之后,我尝试从其他程序添加 p 值:
stat.test <- tibble::tribble(
~group1, ~group2, ~p.adj, ~p.signif,
"ILL", "JXD", 6.466374e-01, 'n.s',
"ILL", "NP", 5.301167e-50, '****'
)
ggplot(df, aes(method, value, fill = method)) + # This is the plot function
geom_violin() +
geom_boxplot(width=0.2, fill="white", alpha = 0.1) +
labs(x = "Method", fill="Method") +
stat_pvalue_manual(
stat.test,
y.position = 900, step.increase = 1,
label = "p.adj"
)
但出现以下错误:
Error in FUN(X[[i]], ...) : object 'method' not found
我尝试改用 ggboxplot 函数,通过在引号 'method' 之间加上它可以正常工作,这不适用于 ggplot 函数。但是使用前者我无法得到我想要的数字。
g <- ggboxplot(df, x = "method", y = "value", width = 0.8)
g+ stat_pvalue_manual(
stat.test,
y.position = 900, step.increase = 0.7,
label = "p.signif"
我不明白哪里错了。
非常感谢!
问题是您将 fill=method
指定为全球美学。因此,在 stat_pvalue_manual
中也在您的数据框 stat.test
中寻找列名 method
。
要解决此问题,请将 fill=method
设为 geom_violin
的本地美学:
library(ggplot2)
library(ggpubr)
df <- data.frame(
method = c(rep("JXD", 100), rep("ILL", 100)),
value = c((runif(100, min = 400, max = 800)), runif(100, min = 500, max = 850))
)
stat.test <- tibble::tribble(
~group1, ~group2, ~p.adj, ~p.signif,
"ILL", "JXD", 6.466374e-01, "n.s",
"ILL", "NP", 5.301167e-50, "****"
)
ggplot(df, aes(method, value)) + # This is the plot function
geom_violin(aes(fill = method)) +
geom_boxplot(width = 0.2, fill = "white", alpha = 0.1) +
labs(x = "Method", fill = "Method") +
stat_pvalue_manual(
stat.test,
y.position = 900, step.increase = 1,
label = "p.adj"
)