我知道有很多问题已经开放,可以在刻面图中添加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"))
现在我想在每个图中添加显着性水平,在两个温度中分别比较寄生和宿主杀戮的结果。所以每个情节都有6个对比。我使用函数stat_compare_means测试了该选项。但是,正如本Rblogger教程(https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/)中所建议的那样,我应该列出一个列表来说明我想要比较的内容。但是,在这种情况下,我有两个25°C的结果和两个30°C的结果。如何创建这个列表提到所有对比? 如果有人能为我澄清我如何解决这个问题,那将对我有所帮助。谢谢。
答案 0 :(得分:1)
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")
或者,切换x和填充变量:
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")
答案 1 :(得分:0)
感谢@thc的帮助和耐心。当我决定在错误栏上使用字母时,我必须为每个字母创建一个新的data.frame并添加geom_text
。在测试了许多x和y值以将字母插入正确的点之后,这使得下面的代码正确。但肯定有一种更简单的方法,我不知道它。
sigvals1 <- data.frame(x=0.75,y=222.4, text=c("a"), behavior=c("Parasitism"))
sigvals2 <- data.frame(x=1.207,y=190.55, text=c("a"), behavior=c("Parasitism"))
sigvals3 <- data.frame(x=1.75,y=117.7, text=c("b"), behavior=c("Parasitism"))
sigvals4 <- data.frame(x=2.209,y=103, text=c("b"), behavior=c("Parasitism"))
sigvals5 <-data.frame(x = 0.75, y = 74.8, text=c("A"), behavior = c("Host-Killing"))
sigvals6 <-data.frame(x = 1.20, y = 92.97, text=c("B"), behavior = c("Host-Killing"))
sigvals7 <-data.frame(x = 1.74, y = 49.8, text=c("C"), behavior = c("Host-Killing"))
sigvals8 <-data.frame(x = 2.196, y = 43, text=c("C"), behavior = c("Host-Killing"))
plot +
geom_text(data=sigvals1, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals2, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals3, aes(x=x,y=y, label=text, fill=NA), hjust=0)+
geom_text(data=sigvals4, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals5, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals6, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals7, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals8, aes(x=x,y=y, label=text, fill=NA), hjust=0)