我有一个比例图,我想添加显着性条,以显示各组之间的统计差异。我想使用该ggsignif
包创建类似的内容:
我已经尝试过使用ggsignif
包和ggplot2
,但是结果似乎不适用于比例测试(例如chi.square)
我的数据如下所示:
Input =("
Group Yes No
1 10 90
2 30 70
3 20 80
")
test_table = as.data.frame(read.table(textConnection(Input),
header=TRUE))
Run Code Online (Sandbox Code Playgroud)
我的最初情节看起来像这样:
ggplot(test_table,
aes(x=Group, y=Yes))
+ geom_col()
Run Code Online (Sandbox Code Playgroud)
这是一种可能性。
我们首先使用基数R来计算成对的比例对之间的成对比较(针对多个假设检验的校正)pairwise.prop.test
(请参阅?pairwise.prop.test
和?prop.test
了解详细信息):
library(broom)
library(tidyverse)
res <- pairwise.prop.test(as.matrix(test_table[, -1])) %>%
tidy() %>%
mutate_at(vars(contains("group")), ~factor(.x, test_table$Group))
res
## A tibble: 3 x 3
# group1 group2 p.value
# <fct> <fct> <dbl>
#1 2 1 0.00235
#2 3 1 0.149
#3 3 2 0.149
Run Code Online (Sandbox Code Playgroud)
我broom::tidy
用来整理输出pairwise.prop.test
;这不是关键的依赖关系,但可以节省我们一些时间。
然后,我们可以绘制Yes/(Yes + No)
成对测试比例测试中的比例和覆盖p值
library(ggsignif)
df <- test_table %>%
mutate(Prop = Yes / (Yes + No))
ggplot(df, aes(Group, Prop, group = Group)) +
geom_col() +
geom_signif(
xmin = as.integer(res$group1),
xmax = as.integer(res$group2),
y_position = 0.3 + df$Prop,
annotation = format(res$p.value, digits = 3),
tip_length = 0.1)
Run Code Online (Sandbox Code Playgroud)