在 ggplot2 #2 的各个方面注释文本

Annotating text on individual facets in ggplot2 #2

我在将文本注释到 ggplot2 中的各个方面时也遇到了一些麻烦(参考相关 post:Annotating text on individual facet in ggplot2)。

数据框:

str(cfit_2)
'data.frame':   186 obs. of  5 variables:
 $ Participant: Factor w/ 31 levels "2","3","4","5",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Condition  : Factor w/ 2 levels "Active control group",..: 1 2 2 2 1 1 2 2 1 1 ...
 $ Time       : Factor w/ 2 levels "Pretest","Posttest": 1 1 1 1 1 1 1 1 1 1 ...
 $ CFIT       : num  10 13 17 11 19 15 19 11 15 16 ...
 $ Version    : Factor w/ 3 levels "Total CFIT","CFIT version 1",..: 1 1 1 1 1 1 1 1 1 1 ...

代码:

p<-ggplot(cfit_2,aes(Time,CFIT,fill=Condition))+
  scale_y_continuous(breaks=1:20)+
  scale_fill_manual(values=c("white","lightgrey"))+
  geom_violin()+
  theme_classic()+
  coord_cartesian(ylim=c(1, 20),xlim=c(1, 2))+
  theme(axis.line=element_blank())+
  facet_grid(.~Version)+ylab("CFIT raw score")+

  geom_segment(x=.3925,xend=.3925,y=1,yend=20)+
  geom_segment(x=1,xend=2,y=.015,yend=.015)+

 stat_summary(fun.y=mean,geom="point",position=position_dodge(w=.9))+
 stat_summary(fun.data=mean_cl_boot,geom="errorbar",position=position_dodge(w=.9),width=0)+

   geom_segment(data=data.segm_1,aes(x=x,xend=xend,y=y,yend=yend),inherit.aes=FALSE)+
   geom_segment(data=data.segm_2,aes(x=x,xend=xend,y=y,yend=yend),inherit.aes=FALSE)+
   geom_segment(data=data.segm_3,aes(x=x,xend=xend,y=y,yend=yend),inherit.aes=FALSE)+
   geom_segment(data=data.segm_1_2,aes(x=x,xend=xend,y=y,yend=yend),inherit.aes=FALSE)+
   geom_segment(data=data.segm_2_2,aes(x=x,xend=xend,y=y,yend=yend),inherit.aes=FALSE)+
   geom_segment(data=data.segm_3_2,aes(x=x,xend=xend,y=y,yend=yend),inherit.aes=FALSE)

Violin plot

我想做的是在每个段旁边添加 "NS" 或“*”。使用以下数据框:

ann_text<-data.frame(Time=c("Pretest","Posttest","Pretest","Posttest","Pretest","Posttest"),CFIT=c(8,7,3,2,2,3),
       lab=c("NS","*","NS","*","NS","*"),
       Version=factor(c("Total CFIT","Total CFIT","CFIT version 1","CFIT version 1","CFIT version 2","CFIT version 2"),
                levels=c("Total CFIT","CFIT version 1","CFIT version 2")))

ann_text:

Time CFIT lab        Version
1  Pretest    8  NS     Total CFIT
2 Posttest    7   *     Total CFIT
3  Pretest    3  NS CFIT version 1
4 Posttest    2   * CFIT version 1
5  Pretest    2  NS CFIT version 2
6 Posttest    3   * CFIT version 2

...我从 p+geom_text(data=ann_text,aes(label=lab)):

得到以下结果
**Error in eval(expr, envir, enclos) : object 'Condition' not found**

将 aes(fill=Condition) 移动到 geom_violin() 得到以下图:

Violin plot #2

最简单的方法是告诉 geom_text 不要使用 Condition 进行填充,方法是将其设置为 NULL。这样,您可以将 fill 保留在 ggplot 调用中并将其应用于所有其他 geoms,而无需每次都指定它。

创建数据(因为您没有提供任何数据)

cfit_2 <- data.frame(
  Time = c('Pretest', 'Posttest'),
  Condition = rep(c('Active control group', 'Training group'), each = 2),
  Version = rep(c('Total CFIT', 'CFIT version 1', 'CFIT version 2'), each = 40),
  CFIT = rnorm(120, 10, 3)
)

使用你的绘图代码

p<-ggplot(cfit_2,aes(Time,CFIT,fill=Condition))+
  #scale_y_continuous(breaks=1:20)+
  scale_fill_manual(values=c("white","lightgrey"))+
  geom_violin()+
  theme_classic()+
  #coord_cartesian(ylim=c(1, 20),xlim=c(1, 2))+
  theme(axis.line=element_blank())+
  facet_grid(.~Version)+ylab("CFIT raw score")+

  geom_segment(x=.3925,xend=.3925,y=1,yend=20)+
  geom_segment(x=1,xend=2,y=.015,yend=.015)+

  stat_summary(fun.y=mean,geom="point",position=position_dodge(w=.9))+
  stat_summary(fun.data=mean_cl_boot,geom="errorbar",position=position_dodge(w=.9),width=0)

(请注意,我注释掉了关于轴的两行,因为它们给我带来了麻烦。我还删除了注释行,因为您没有为那些提供数据。)

添加文字

首先在需要星号和 "NS"es 的位置创建坐标。

ann_text<-read.table(text="
Time CFIT lab        Version
Pretest    15  NS     'Total CFIT'
Posttest    7   *     'Total CFIT'
Pretest    3  NS 'CFIT version 1'
Posttest    2   * 'CFIT version 1'
Pretest    2  NS 'CFIT version 2'
Posttest    3   * 'CFIT version 2'
",header=T)

然后注释。

p + geom_text(data = ann_text, aes(label = lab, fill = NULL))

结果

当然,文本看起来不太好(它在错误的 y 级别),但那是因为我的数据不同。