在ggplot2#2中对各个方面上的文本进行注释

时间:2016-01-05 10:04:57

标签: r ggplot2 annotations facet

我在ggplot2中为单个方面添加文本注释时遇到了一些麻烦(参考相关帖子: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

1 个答案:

答案 0 :(得分:5)

最简单的方法是通过将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) ,而无需每次都指定。{/ p>

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

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))

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

添加文字

首先创建您想要星号的坐标和&#34; NS&#34; es。

{{1}}

然后注释。

{{1}}

结果

enter image description here

当然,文字看起来并不好(它处于错误的y级别),但这是因为我的数据不同。