我在ggplot2中的各个方面注释文本时也遇到了一些麻烦(参考相关文章: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)
小提琴情节
我想做的是在每个句段旁边添加“ 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()会得到以下曲线:
小提琴情节#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”的位置创建坐标。
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级别错误),但这是因为我的数据不同。