r - 在具有相同 x 轴的两个结果的多面图中添加显着性水平

标签 r ggplot2 annotate geom-text ggpubr

我知道已经有很多问题可以在多面图中的 ggplot2 中添加显着性水平。但是,作为 R 初学者,我没有找到适合我的情节的解决方案。 我的数据(.txt 文件)可在此链接中找到:

1drv.ms/t/s!AsLAxDXdkA9Mg8oXdJ-qxD5AeB4KAw

有四列:三个因素水平(温度、拟寄生物种类和行为)和一个数字水平(寄生数量和宿主杀伤)。

我用代码运行绘图:

    ggplot(mydata, aes(x = temperature, y = value, fill = species)) +  
facet_grid(. ~ behavior) +
stat_summary(fun.y = mean, geom = "bar", position = "dodge", stat="identity") +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1) +
labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
theme(legend.position = "bottom", legend.title = element_blank(),  legend.text = element_text(size = 11, face = "italic"))

enter image description here

现在我想在每个图中添加显着性水平,分别比较两种温度下寄生和宿主杀伤的结果。所以我在每个情节中有 6 个对比。我使用函数 stat_compare_means 测试了该选项。然而,正如 Rblogger 教程 ( https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/ ) 中所建议的那样,我应该列出一个列表来说明我想要比较的内容。但是,在这种情况下,我有两个 25°C 的结果和两个 30°C 的结果。我如何创建这个列表来提及所有对比? 如果有人可以为我澄清如何解决这个问题,那将对我有很大帮助。谢谢。

最佳答案

stat_compare_means “开箱即用”(这是有道理的,因为它使用了 ggplot 函数中定义的分组。

ggplot(mydata, aes(x = temp, y = value, fill = factor(species))) +  
  facet_grid(. ~ behavior) +
  stat_summary(fun.y = mean, geom = "bar", position = position_dodge(width = 1), stat="identity") +
  stat_summary(fun.data = mean_sdl, geom="errorbar", position = position_dodge(width = 1), width=0.25, stat="identity", fun.args = list(mult = 1)) +
                 labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
                 theme(legend.position = "bottom", legend.title = element_blank(),  legend.text = element_text(size = 11, face = "italic")) + 
  stat_compare_means(method = "t.test")

enter image description here

或者,切换 x 和 fill 变量:

ggplot(mydata, aes(x = factor(species), y = value, fill = factor(temp))) +  
  facet_grid(. ~ behavior) +
  stat_summary(fun.y = mean, geom = "bar", position = position_dodge(width = 1), stat="identity") +
  stat_summary(fun.data = mean_sdl, geom="errorbar", position = position_dodge(width = 1), width=0.25, stat="identity", fun.args = list(mult = 1)) +
  labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
  theme(legend.position = "bottom",  legend.text = element_text(size = 11, face = "italic")) + 
  stat_compare_means(method = "t.test")

enter image description here

关于r - 在具有相同 x 轴的两个结果的多面图中添加显着性水平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48587854/

相关文章:

r - 如何调整geom_point()的scale_alpha?

R:如何使用文本框注释 ggplot?

r - R 中的 ggplot2 : annotate outside of plot and underline text

R:mapped_discrete` 对象只能从数字向量创建

c++ - 将 StringVector 与 Rcpp 连接起来

r - 使用时间序列数据在 ggplot 中创建垂直线

r - 无法在点和点密度上映射多边形

r - 更改文本注释的背景颜色以增加对比度和可见性

R:根据观察窗口一定分钟数内的时间对数据框进行子集化

r - 使用R获取热图中垃圾箱的数量