gpt4 book ai didi

r - ggplot2:将 p 值添加到分组箱线图

转载 作者:行者123 更新时间:2023-12-02 03:31:14 25 4
gpt4 key购买 nike

我正在尝试使用“stat_signif”函数将 p_values 添加到我的图表中。
问题是我的箱线图是分组箱线图,我想在其中比较同一类别的每 2 个箱线图,而 stat_signif 函数需要 x 轴值进行比较。
这是我的代码:

p <- ggplot(plot.data, aes(x = Element, y = Value, fill = Group)) + #Define the elements for plotting - group by "strandness".
geom_boxplot(outlier.shape = NA, colour = "black") +
scale_fill_manual(values = c("goldenrod","darkgreen")) +
coord_cartesian(ylim = c(0, 0.03)) +
stat_summary(fun.y=mean, colour="black", geom ="point", shape=18, size=4 ,show.legend = FALSE, position = position_dodge(0.75)) +
theme(legend.title=element_blank(),legend.text = element_text(size=16), axis.text.x = element_text(color = "black", size = 12), axis.text.y = element_text(color = "black", size = 12),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=0.5),
legend.key = element_rect(colour = "transparent", fill = "white")) +
theme(plot.title = element_text(lineheight=.8, hjust = 0.5, size = 20),axis.title.y = element_text(size = 20, angle = 90, margin = margin(t = 0, r = 20, b = 0, l = 0))) +
labs(x = "", y = paste0(dinuc, " frequency")) +
theme(plot.margin = unit(c(2,1,1,1), "cm")) +
#stat_compare_means(aes(group = group))
stat_signif(comparisons = list(c("Genes", "mRNA"))
,test = "wilcox.test", test.args = list(paired = FALSE, exact = FALSE, correct = FALSE,
map_signif_level = T), y_position = 0.02)

plot.data 数据框的样子:

  Group, Value, Element
1 Transcribed, 0.004814926, Genes
2 Non-transcribed, 0.008926, Genes
3 Transcribed, 0.086000026, mRNA
4 Non-transcribed, 0.00548, mRNA
5 Transcribed, 0.258400078, Exons
6 Non-transcribed, 0.23008457, Exons
7 Transcribed, 0.00005687, Introns
8 Non-transcribed, 0.890000521, Introns

等(对于每个元素,大约有 10000 行)

这是代码得到的图: plot当我真的想比较每个元素的转录和非转录箱形图时。

最佳答案

您没有发布足够的数据来获取 p 值,所以我发布了一个示例,您可以根据自己的数据集进行调整:

library(tidyverse)
library(ggpubr)

mtcars %>%
mutate_at(vars(am, cyl), as.factor) %>%
ggplot(aes(cyl, disp, fill=am))+
geom_boxplot()+
stat_compare_means(aes(group = am))

enter image description here

如果您只想在绘图中包含 p 值,则可以使用stat_compare_means(aes(group = am), label = "p.format")

关于r - ggplot2:将 p 值添加到分组箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51807337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com