【问题标题】:How to add significance bar between subgroups of box plot如何在箱线图的子组之间添加显着性条
【发布时间】:2019-04-14 11:17:53
【问题描述】:

我想进行 wilcox 检验并为箱线图中的每个组(而不是组之间)添加一个显着性条。 IE。在时间 2、6 和 14 比较子组(“0”、“1”)。 这是我目前所拥有的:

WS = 时间
DV = 效果
count = 子组(“0”或“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

Output from code

添加时

p <- p + geom_signif(comparisons = list(c("0", "1")),
              map_signif_level=TRUE,test='wilcox.test')

到上面,我得到以下错误:

Error in f(...) : 
  Can only handle data with groups that are plotted on the x-axis

我假设只有当数据在 x 轴上时才能进行比较。但是,我想保持情节几乎与现在一样,x 轴上的时间为 2、6 和 14。我该如何解决这个问题?

【问题讨论】:

  • 您能提供一些示例数据吗?您可以发布dput(data)dput(head(data))的输出。
  • Simon,这是我的数据的 dput()。结构(列表(WS = c(2L,6L,14L,2L,6L,2L,6L,14L,2L,6L,2L,6L,14L,2L,6L,14L,2L,2L,6L,2L,6L, 14L, 2L, 6L, 14L ), DV = c(19.87, 17.58, 3.87, 28, 29.68, 22.57, 26.73, 8.75, 13.19, 0.53, 39.63, 41.96, 18.14, 35.01, 35.5, 11.3. , 8.55, 3.86, 0.86, 18.97, 14.04, 1.99), count = c("1", "1", "1", "1", "1", "0", "0", "0", “1”、“1”、“1”、“1”、“1”、“1”、“1”、“1”、“1”、“1”、“1”、“1”、“1” ", "1", "0", "0", "0")), row.names = c(NA, -25L), class= "data.frame")

标签: r ggplot2 boxplot


【解决方案1】:

来自ggsignif 的小插图(请参阅??ggsignif)我知道对于使用position='dodge' 的图层(这是您的箱线图的情况,因为您有带交互的分组)您需要提供自己定位和标注(标签)。

您可以像这样计算所有值:

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

这给出:

【讨论】:

  • 如果你的x轴是分类的怎么办?
  • 手动操作时可能是一个传奇
  • @BrianWiley:您可以通过将 x 位置 fpr 因子(分类变量)转换为它们的数值表示来执行数学运算:p &lt;- p + geom_signif(y_position = y.values, xmin = unique(as.numeric(data$WS))-.4, xmax = unique(as.numeric(data$WS))+.4, annotations = labels)(这将在转换 data$WS &lt;- as.factor(data$WS) 后起作用)
  • 谢谢,我发现了。现在我正在尝试隐藏括号(如果不重要)并添加图例。也许需要不同的包
  • 由于您手动提供值,因此您可以删除那些不重要的值。 n.s &lt;- which(p.values&gt;=.05) if (length(n.s)&lt;length(p.values)) { p.values &lt;- p.values[-n.s] labels &lt;- labels[-n.s] y.values &lt;- y.values[-n.s] x.values &lt;- x.values[-n.s] p &lt;- p + geom_signif(y_position = y.values, xmin = x.values-.19, xmax = x.values+.19, annotations = labels) }
猜你喜欢
  • 2020-11-08
  • 2018-03-06
  • 2016-01-26
  • 2018-08-09
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
相关资源
最近更新 更多