如何在箱形图的子组之间添加显着性条 [英] How to add significance bar between subgroups of box plot
问题描述
我想进行一次wilcox测试,并在箱图中为每个组(而不是组之间)添加一个显着性条.IE.比较时间2、6和14的子组("0","1").这是我到目前为止的内容:
WS =时间
DV =效果
count =子组("0"或"1")
p<-ggplot(数据,aes(x = WS,y = DV,group = count))p<-p + geom_boxplot(aes(fill = factor(count),group = interaction(WS,count)))p<-p + stat_summary(fun.y = median,geom ="smooth",aes(group = factor(count),color = factor(count)))p<-p + scale_x_continuous(breaks = c(2,6,14))p
I would like to make a wilcox test and add a significance bar for each group in the boxplot (not between groups). I.e. comparing subgroups ("0", "1") at time 2, 6, and 14. This is what I have so far:
WS = time
DV = effect
count = subgroup ("0" or "1")
p <- ggplot(data, aes(x=WS, y=DV, group=count))
p <- p + geom_boxplot(aes(fill=factor(count), group=interaction(WS, count)))
p <- p + stat_summary(fun.y=median, geom="smooth", aes(group=factor(count), color =factor(count)))
p <- p + scale_x_continuous(breaks = c(2,6,14))
p
When adding
p <- p + geom_signif(comparisons = list(c("0", "1")),
map_signif_level=TRUE,test='wilcox.test')
to the above, I get the following error:
Error in f(...) :
Can only handle data with groups that are plotted on the x-axis
I assume that a comparison can only be made on if the data is on the x-axis. However, I want to keep the plot pretty much as it is right now, with time 2, 6 and 14 on the x-axis. How can I solve this?
from the vignette of ggsignif
(see ??ggsignif
) I understand that for layers that use position='dodge'
(which is the case for your boxplots as you have the grouping with an intercation) you'll need to provide the position and the annotation (label) yourself.
You can calculate all the values like this:
p <- ggplot(data, aes(x=WS, y=DV, group=count))
p <- p + geom_boxplot(aes(fill=factor(count), group=interaction(WS, count)))
p <- p + stat_summary(fun.y=median, geom="smooth", aes(group=factor(count), color =factor(count)))
p <- p + scale_x_continuous(breaks = c(2,6,14))
p.values <- sapply(split(data, data$WS), function(x){wilcox.test(DV~count, x)$p.value})
labels <- symnum(p.values, corr = FALSE, cutpoints = c(0, .001,.01,.05, 1), symbols = c("***","**","*","n.s."))
y.values <- sapply(split(data, data$WS), function(x){max(sapply(split(x, x$count), function(xx){boxplot(x$DV, plot=F)$stats[5, ]}))})+2
p <- p + geom_signif(y_position = y.values, xmin = unique(data$WS)-.4, xmax = unique(data$WS)+.4, annotations = labels)
p
This gives:
这篇关于如何在箱形图的子组之间添加显着性条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!