问题描述
这是我第一次尝试 R。我想使用 stat_compare_means 来比较子组内的均值以及来自不同子组的均值。我需要用治疗标记的 x 轴。 I am aiming for some thing like this
我的数据如下
A tibble: 9 x 3
Treatment Group Count
<chr> <chr> <dbl>
1 + a 12
2 + a 13
3 + a 11
4 + b 14
5 + b 15
6 + b 15
7 - c 22
8 - c 24
9 - c 21
我用 ggbarplot 和 geom_bar 尝试了几次,但都没有奏效。 stat_compare_means 甚至可以实现我的计划吗?任何帮助表示赞赏。
我最后一次尝试是这样的:
library(ggpubr)
library(ggplot2)
df <- structure(list(Treatment = c("+","+","-","-"),Group = c("a","a","b","c","c"),Count = c(12,13,11,14,15,22,24,21)),row.names = c(NA,-9),class = "data.frame")
ggbarplot(df,x = "Treatment",y = "Count",add = "mean_se",error.plot = "upper_errorbar",color = "Group",position = position_dodge(0.8))+
stat_compare_means(df,comparisons = list(c("a","b" ),c("a",c("b","c")),label = "p.signif")
哪个返回
Computation failed in `stat_signif()`:
Missing value,where TRUE/FALSE is needed
解决方法
尝试这样的事情:
library(ggpubr)
library(ggplot2)
#Comp
my_comparisons = list( c("+.a","+.b"),c("+.a","-.c"),c("+.b","-.c"))
#Plot
ggplot(df,aes(x=interaction(Treatment,Group),y=Count,fill=Group,group=Group))+
geom_boxplot(position = position_dodge2(width = 0.5,preserve = 'single'))+
theme_bw()+
stat_compare_means(comparisons = my_comparisons,method = 't.test')+
theme(legend.position = 'top',panel.grid = element_blank(),axis.text = element_text(face='bold',color='black'),axis.title = element_text(face='bold',strip.text = element_text(face='bold',legend.text = element_text(face='bold',legend.title = element_text(face='bold',plot.title = element_text(face='bold',color='black',hjust=0.5))+
ggtitle('My plot')
输出:
使用的一些数据:
#Data
df <- structure(list(Treatment = c("+","+","-","-"),Group = c("a","a","b","c","c"),Count = c(12L,13L,11L,14L,15L,22L,24L,21L)),row.names = c(NA,-9L),class = "data.frame")