如何在R上使用躲避的geom_boxplot为每个构面注释不同的值? [英] How to annotate different values for each facet with dodged geom_boxplot on R?
问题描述
我试图使用组(填充)和构面在ggplot箱图中添加显着性星号。
I am trying to add significance asterisks to my ggplot boxplot, using groups (fill) and facets.
使用 geom_signif()
我可以添加诸如以下的栏:
Using geom_signif()
I can add bars such as:
我也尝试对躲避的箱线图进行同样的操作。类似于
I am trying to do the same for the dodged boxplots too.. similar to
(假设存在有意义的值在较小的线条上方...)
(Imagine there were significance values above the smaller lines...)
前一个图形的代码:
数据:
library(ggplot2)
library(ggsignif)
df <- data.frame(iris,petal.colour=c("red","blue"), country=c("UK","France","France"))
第一个图:
ggplot(df, aes(country,Sepal.Length))+
geom_boxplot(position="dodge",aes(fill=petal.colour))+
facet_wrap(~Species, ncol=3)+
geom_signif(comparisons = list(c("France", "UK")), map_signif_level=TRUE,
tip_length=0,y_position = 9, textsize = 4)
,对于较小的柱形
+geom_signif(annotations = c("", ""),
y_position = 8.5,
xmin=c(0.75,1.75), xmax=c(1.25,2.25),tip_length=0)
让R完成工作会很棒,但是如果手动在这些较小的行上方添加文本会更容易,那么我也很好。
It would great to let R do the work, but if its easier to manually add text above these smaller lines then that's fine with me.
推荐答案
我不知道如何使用 geom_signif
。有关我的尝试,请参见第一部分。我能够使用 ggpubr
和 stat_compare_means
使其工作,我相信这是<$ c $的扩展c> geom_signif 。
I can't figure out how to get them to work for that group using geom_signif
. See the first part for my attempt. I was able to get it to work using ggpubr
and stat_compare_means
, which I believe is an extension of geom_signif
.
ggplot(df, aes(country,Sepal.Length)) +
geom_boxplot(position="dodge",aes(fill=petal.colour)) +
facet_wrap(~Species, ncol=3) +
geom_signif(comparisons = list(c("France", "UK")), map_signif_level=TRUE,
tip_length=0,y_position = 9, textsize = 4) +
geom_signif(y_position = 8.5,
xmin=c(0.75,1.75), xmax=c(1.25,2.25), tip_length=0, map_signif_level = c("***" = 0.001, "**" = 0.01, "*" = 0.05))
Warning messages:
1: In wilcox.test.default(c(4.9, 4.7, 5, 5.4, 5, 4.4, 5.4, 4.8, 4.3, :
cannot compute exact p-value with ties
2: In wilcox.test.default(c(7, 6.9, 5.5, 5.7, 6.3, 6.6, 5.2, 5.9, 6, :
cannot compute exact p-value with ties
3: In wilcox.test.default(c(6.3, 5.8, 6.3, 6.5, 4.9, 7.3, 7.2, 6.5, :
cannot compute exact p-value with ties
4: Computation failed in `stat_signif()`:
arguments imply differing number of rows: 6, 0
5: Computation failed in `stat_signif()`:
arguments imply differing number of rows: 6, 0
6: Computation failed in `stat_signif()`:
arguments imply differing number of rows: 6, 0
使用 ggpubr
和 stat_compare_means
。请注意,您可以使用不同的标签和测试等。请参见?stat_compare_means
。
Using ggpubr
and stat_compare_means
. Note you can use different labels, and tests, etc. See ?stat_compare_means
.
library(ggpubr)
ggplot(df, aes(country,Sepal.Length)) +
geom_boxplot(position="dodge",aes(fill=petal.colour)) +
facet_wrap(~Species, ncol=3) +
stat_compare_means(aes(group = country), label = "p.signif", label.y = 10, label.x = 1.5) +
stat_compare_means(aes(group = petal.colour), label = "p.format", label.y = 8.5)
这篇关于如何在R上使用躲避的geom_boxplot为每个构面注释不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!