问题描述
我正在尝试使用 ggsignif
包,并使用以下数据重现 const-ae、here 给出的答案:
library(ggplot2)
library(ggsignif)
counts <- structure(list(ECOST = c("0.52","0.52","0.39","0.26","0.13","0.00","0.00"),group = c("control","treatment","control","treatment"),count = c(18,31,30,35,47,46,66,68,86,86),percentage = c(16.3636363636364,31.9587628865979,27.2727272727273,36.0824742268041,42.7272727272727,47.4226804123711,60,70.1030927835051,78.1818181818182,88.659793814433
),total = c(110,97,110,97),negative_count = c(92,80,62,63,51,44,29,24,11),p_value = c(0.00843644912924255,0.00843644912924255,0.172947686684261,0.497952719783453,0.128982570547408,0.0447500820026408,0.0447500820026408)),row.names = c(NA,-10L),class = c("data.table","data.frame"))
counts %>%
ggplot(aes(x = ECOST,y = percentage,fill = group,label=sprintf("%.02f %%",round(percentage,digits = 1)))) +
geom_col(position = 'dodge') +
geom_text(position = position_dodge(width = .9),# move to center of bars
vjust = -0.5,# nudge above top of bar
size = 4) +
scale_fill_grey(start = 0.8,end = 0.5) +
theme_bw(base_size = 15)
数据:
ECOST group count percentage total negative_count p_value
1: 0.52 control 18 16 110 92 0.0084
2: 0.52 treatment 31 32 97 66 0.0084
3: 0.39 control 30 27 110 80 0.1729
4: 0.39 treatment 35 36 97 62 0.1729
5: 0.26 control 47 43 110 63 0.4980
6: 0.26 treatment 46 47 97 51 0.4980
7: 0.13 control 66 60 110 44 0.1290
8: 0.13 treatment 68 70 97 29 0.1290
9: 0.00 control 86 78 110 24 0.0448
10: 0.00 treatment 86 89 97 11 0.0448
所以下一步是将显着性水平添加到图中。我一直在弄乱它,但我不太了解代码以适应它。我知道我必须覆盖绘图数据,但我不知道该怎么做。
counts %>%
ggplot(aes(x = ECOST,end = 0.5) +
theme_bw(base_size = 15) +
geom_signif(stat="identity",data=data.frame(x=c(0.875,1.875),xend=c(1.125,2.125),y=c(5.8,8.5),annotation=c("**","NS")),aes(x=x,xend=xend,y=y,yend=y,annotation=annotation)) +
geom_signif(comparisons=list(c("treatment","control")),annotations="***",y_position = 9.3,tip_length = 0,vjust=0.4)
如果有人能向我解释如何进行控制与治疗,也许是两个水平的 ECOST 之间的一个,我会非常高兴,之后我相信我可以从那里接受它。
预期结果(取自链接):
解决方法
我不确定您想在图表中添加什么“显着水平”。不过,这可能有助于您了解如何添加自己的内容。
counts %>%
ggplot(aes(x = ECOST,y = percentage,fill = group)) +
geom_col(position = 'dodge') +
scale_fill_grey(start = 0.8,end = 0.5) +
theme_bw(base_size = 15) +
## between ESCOT levels
geom_signif(annotation=c("**","NS"),y_position = c(95,75),xmin=c(1,2),xmax=c(2,5)) +
geom_signif(annotation=c("***","*"),y_position = c(50,40),xmin=c(3.75,4.75),xmax=c(4.25,5.25)) ## Within ESCOT levels but between groups
y_poistion
是您想要绘制的 y 值
xmin
是开始括号位置的向量,因此在示例中 **
括号从第一组条开始,ns
括号从第二组开始
xmax
与 xmin
相同,但括号结束。
你也不需要两个geom_signif
层,我只是把它们做成两个来显示组之间和级别之间的差异。
+ geom_signif(annotation=c("**","NS","***",75,50,2,3.75,5,4.25,5.25))