为什么 stat_compare_means 只在一个方面显示成对比较?
Why does stat_compare_means only show pairwise comparisons in one facet?
我正在尝试对分组数据进行成对比较。我以前使用 stat_compare_means 成功地做到了这一点,但出于某种原因,这次它只在其中一个方面面板中显示比较栏。我试过了,但似乎无法让它发挥作用。我在下面提供了一个简化的工作示例,其中只有两个条件。真实数据具有相同数量的集合,但条件更多。非常感谢任何帮助!
data <- data.frame(sample = seq(1:83),
condition = c("KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO",
"KO", "KO", "KO", "KO", "KO", "KO", "KO", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT"),
set = c("di", "tet", "mono", "di", "tet", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono"),
score = c(0, 0, 0, -5.95940584704586, 10.0932562947815, -3.16676604569923,
4.46133814098881, -1.9888611720281, -7.08194974795108, 3.17097628218171,
-1.83986980857496, -0.843268716519414, 1.40178526106758, -0.340339302553342,
-3.76913603338144, 4.28943971347741, -3.20197704274428, -3.54168755452774,
15.414510676737, -5.85333426479177, -1.87949902971026, 14.1424002410594,
-2.14726619139082, -5.01378228438499, 11.1131227496058, 0.097013584879446,
-6.55527134311774, 12.2234175105232, -2.55259067519978, -6.68392512983342,
15.8358484731832, -2.27825891764331, -5.40451835097939, 11.2240240941934,
-1.53075128507785, -4.80008896082703, 15.3667539728667, -4.81370852797055,
-2.69976280917806, 21.9926791189896, 0.61798090190648, -8.68663007652496,
13.8852585926079, -0.80329005076484, -16.8459570277756, 13.3500356549569,
-5.59531483186873, -3.33602772657725, 11.338954967882, -2.12614700145763,
-3.37418493461362, 11.5903340330526, -2.87224785160433, -14.7792521265679,
7.7542705233175, -2.06876649679246, -1.31032740187699, 17.6666627835987,
-0.824420163207606, -4.82659116096503, -0.0626028094479509, -1.90431942338018,
-20.3180652520285, 6.83632028067972, -1.58570945276274, -2.77261532150261,
2.98168865160908, -1.89922364414076, -5.80584721371712, 7.13599592922731,
-1.23964885854847, -5.1236580504364, 14.5532078112838, -3.82017971494402,
-4.39741499313133, 11.7342969461195, -2.5852194454582, -3.18458897513284,
12.7762556862722, 1.1245622869403, -2.98430760077172, 13.981837061262,
-1.19532384849617))
ggplot(data aes(x=condition, y=score, fill=set)) +
stat_summary(fun=mean, geom="bar", colour="black", alpha=1) +
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.25) +
geom_point(color="black", shape=21, show.legend=TRUE, size=3, stroke=1) +
facet_wrap(~set, scales="free") +
stat_compare_means(method = "t.test", comparisons=list(c("KO", "WT")), hide.ns=F) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(expand = expansion(mult = c(0.2, 0.2)))
我认为你应该将 group = condition
添加到 ggplot
中的 aes
。
ggplot(temp, aes(x=condition, y=score, color = set, fill=set, group = condition)) +
stat_summary(fun=mean, geom="bar", colour="black", alpha=1) +
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.25) +
geom_point(color="black", shape=21, show.legend=TRUE, size=3, stroke=1) +
facet_wrap(~set, scales="free") +
stat_compare_means(method = "t.test", comparisons=list(c("KO", "WT")), hide.ns=F) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(expand = expansion(mult = c(0.2, 0.2)))
您也可以手动计算 p 值,然后使用 ggsignif package 将它们放在图上。这需要更多工作,但您可以调整多重比较的 p 值 and/or 指定您要使用的符号,因此对于更复杂的绘图可能会派上用场,例如
library(tidyverse)
library(ggsignif)
temp <- data.frame(sample = seq(1:83),
condition = c("KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO",
"KO", "KO", "KO", "KO", "KO", "KO", "KO", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT"),
set = c("di", "tet", "mono", "di", "tet", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono"),
score = c(0, 0, 0, -5.95940584704586, 10.0932562947815, -3.16676604569923,
4.46133814098881, -1.9888611720281, -7.08194974795108, 3.17097628218171,
-1.83986980857496, -0.843268716519414, 1.40178526106758, -0.340339302553342,
-3.76913603338144, 4.28943971347741, -3.20197704274428, -3.54168755452774,
15.414510676737, -5.85333426479177, -1.87949902971026, 14.1424002410594,
-2.14726619139082, -5.01378228438499, 11.1131227496058, 0.097013584879446,
-6.55527134311774, 12.2234175105232, -2.55259067519978, -6.68392512983342,
15.8358484731832, -2.27825891764331, -5.40451835097939, 11.2240240941934,
-1.53075128507785, -4.80008896082703, 15.3667539728667, -4.81370852797055,
-2.69976280917806, 21.9926791189896, 0.61798090190648, -8.68663007652496,
13.8852585926079, -0.80329005076484, -16.8459570277756, 13.3500356549569,
-5.59531483186873, -3.33602772657725, 11.338954967882, -2.12614700145763,
-3.37418493461362, 11.5903340330526, -2.87224785160433, -14.7792521265679,
7.7542705233175, -2.06876649679246, -1.31032740187699, 17.6666627835987,
-0.824420163207606, -4.82659116096503, -0.0626028094479509, -1.90431942338018,
-20.3180652520285, 6.83632028067972, -1.58570945276274, -2.77261532150261,
2.98168865160908, -1.89922364414076, -5.80584721371712, 7.13599592922731,
-1.23964885854847, -5.1236580504364, 14.5532078112838, -3.82017971494402,
-4.39741499313133, 11.7342969461195, -2.5852194454582, -3.18458897513284,
12.7762556862722, 1.1245622869403, -2.98430760077172, 13.981837061262,
-1.19532384849617))
anno_df = compare_means(score ~ condition, group.by = "set", data = temp) %>%
mutate(y_pos = c(3, 24, 2.5))
ggplot(temp, aes(x=condition, y=score, fill=set)) +
stat_summary(fun=mean, geom="bar", colour="black", alpha=1) +
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.25) +
geom_point(color="black", shape=21, show.legend=TRUE, size=3, stroke=1) +
facet_wrap(~set, scales="free") +
geom_signif(data = anno_df, aes(xmin=group1,
xmax=group2,
annotations=p.signif,
y_position = y_pos),
manual = TRUE) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(expand = expansion(mult = c(0.2, 0.2)))
@ricoderks 的答案显然是 easier/better 选项!
我正在尝试对分组数据进行成对比较。我以前使用 stat_compare_means 成功地做到了这一点,但出于某种原因,这次它只在其中一个方面面板中显示比较栏。我试过了,但似乎无法让它发挥作用。我在下面提供了一个简化的工作示例,其中只有两个条件。真实数据具有相同数量的集合,但条件更多。非常感谢任何帮助!
data <- data.frame(sample = seq(1:83),
condition = c("KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO",
"KO", "KO", "KO", "KO", "KO", "KO", "KO", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT"),
set = c("di", "tet", "mono", "di", "tet", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono"),
score = c(0, 0, 0, -5.95940584704586, 10.0932562947815, -3.16676604569923,
4.46133814098881, -1.9888611720281, -7.08194974795108, 3.17097628218171,
-1.83986980857496, -0.843268716519414, 1.40178526106758, -0.340339302553342,
-3.76913603338144, 4.28943971347741, -3.20197704274428, -3.54168755452774,
15.414510676737, -5.85333426479177, -1.87949902971026, 14.1424002410594,
-2.14726619139082, -5.01378228438499, 11.1131227496058, 0.097013584879446,
-6.55527134311774, 12.2234175105232, -2.55259067519978, -6.68392512983342,
15.8358484731832, -2.27825891764331, -5.40451835097939, 11.2240240941934,
-1.53075128507785, -4.80008896082703, 15.3667539728667, -4.81370852797055,
-2.69976280917806, 21.9926791189896, 0.61798090190648, -8.68663007652496,
13.8852585926079, -0.80329005076484, -16.8459570277756, 13.3500356549569,
-5.59531483186873, -3.33602772657725, 11.338954967882, -2.12614700145763,
-3.37418493461362, 11.5903340330526, -2.87224785160433, -14.7792521265679,
7.7542705233175, -2.06876649679246, -1.31032740187699, 17.6666627835987,
-0.824420163207606, -4.82659116096503, -0.0626028094479509, -1.90431942338018,
-20.3180652520285, 6.83632028067972, -1.58570945276274, -2.77261532150261,
2.98168865160908, -1.89922364414076, -5.80584721371712, 7.13599592922731,
-1.23964885854847, -5.1236580504364, 14.5532078112838, -3.82017971494402,
-4.39741499313133, 11.7342969461195, -2.5852194454582, -3.18458897513284,
12.7762556862722, 1.1245622869403, -2.98430760077172, 13.981837061262,
-1.19532384849617))
ggplot(data aes(x=condition, y=score, fill=set)) +
stat_summary(fun=mean, geom="bar", colour="black", alpha=1) +
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.25) +
geom_point(color="black", shape=21, show.legend=TRUE, size=3, stroke=1) +
facet_wrap(~set, scales="free") +
stat_compare_means(method = "t.test", comparisons=list(c("KO", "WT")), hide.ns=F) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(expand = expansion(mult = c(0.2, 0.2)))
我认为你应该将 group = condition
添加到 ggplot
中的 aes
。
ggplot(temp, aes(x=condition, y=score, color = set, fill=set, group = condition)) +
stat_summary(fun=mean, geom="bar", colour="black", alpha=1) +
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.25) +
geom_point(color="black", shape=21, show.legend=TRUE, size=3, stroke=1) +
facet_wrap(~set, scales="free") +
stat_compare_means(method = "t.test", comparisons=list(c("KO", "WT")), hide.ns=F) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(expand = expansion(mult = c(0.2, 0.2)))
您也可以手动计算 p 值,然后使用 ggsignif package 将它们放在图上。这需要更多工作,但您可以调整多重比较的 p 值 and/or 指定您要使用的符号,因此对于更复杂的绘图可能会派上用场,例如
library(tidyverse)
library(ggsignif)
temp <- data.frame(sample = seq(1:83),
condition = c("KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO", "KO",
"KO", "KO", "KO", "KO", "KO", "KO", "KO", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT", "WT",
"WT", "WT", "WT", "WT", "WT", "WT", "WT"),
set = c("di", "tet", "mono", "di", "tet", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono", "di", "tet", "mono", "di", "tet", "mono", "di",
"tet", "mono"),
score = c(0, 0, 0, -5.95940584704586, 10.0932562947815, -3.16676604569923,
4.46133814098881, -1.9888611720281, -7.08194974795108, 3.17097628218171,
-1.83986980857496, -0.843268716519414, 1.40178526106758, -0.340339302553342,
-3.76913603338144, 4.28943971347741, -3.20197704274428, -3.54168755452774,
15.414510676737, -5.85333426479177, -1.87949902971026, 14.1424002410594,
-2.14726619139082, -5.01378228438499, 11.1131227496058, 0.097013584879446,
-6.55527134311774, 12.2234175105232, -2.55259067519978, -6.68392512983342,
15.8358484731832, -2.27825891764331, -5.40451835097939, 11.2240240941934,
-1.53075128507785, -4.80008896082703, 15.3667539728667, -4.81370852797055,
-2.69976280917806, 21.9926791189896, 0.61798090190648, -8.68663007652496,
13.8852585926079, -0.80329005076484, -16.8459570277756, 13.3500356549569,
-5.59531483186873, -3.33602772657725, 11.338954967882, -2.12614700145763,
-3.37418493461362, 11.5903340330526, -2.87224785160433, -14.7792521265679,
7.7542705233175, -2.06876649679246, -1.31032740187699, 17.6666627835987,
-0.824420163207606, -4.82659116096503, -0.0626028094479509, -1.90431942338018,
-20.3180652520285, 6.83632028067972, -1.58570945276274, -2.77261532150261,
2.98168865160908, -1.89922364414076, -5.80584721371712, 7.13599592922731,
-1.23964885854847, -5.1236580504364, 14.5532078112838, -3.82017971494402,
-4.39741499313133, 11.7342969461195, -2.5852194454582, -3.18458897513284,
12.7762556862722, 1.1245622869403, -2.98430760077172, 13.981837061262,
-1.19532384849617))
anno_df = compare_means(score ~ condition, group.by = "set", data = temp) %>%
mutate(y_pos = c(3, 24, 2.5))
ggplot(temp, aes(x=condition, y=score, fill=set)) +
stat_summary(fun=mean, geom="bar", colour="black", alpha=1) +
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.25) +
geom_point(color="black", shape=21, show.legend=TRUE, size=3, stroke=1) +
facet_wrap(~set, scales="free") +
geom_signif(data = anno_df, aes(xmin=group1,
xmax=group2,
annotations=p.signif,
y_position = y_pos),
manual = TRUE) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(expand = expansion(mult = c(0.2, 0.2)))
@ricoderks 的答案显然是 easier/better 选项!