R 在ggplot2#2中注释各个面上的文字

R 在ggplot2#2中注释各个面上的文字,r,ggplot2,annotations,facet,R,Ggplot2,Annotations,Facet,在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

在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)
代码:

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)
。。。我从p+geom_text(data=ann_text,aes(label=lab))得到以下结果:


将aes(填充=条件)移动到geom_()可得到以下曲线图:


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

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


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

aes全局设置中使用的任何变量都会传递到所有其他层。所以在这种情况下,最好将fill=Condition移到geom_小提琴()中,谢谢@Heroka!这很好,但现在我在统计摘要(fun.data=mean\u cl\u boot,geom=“errorbar”,position=position\u dodge(w=0.9),width=0;以上)方面遇到了问题。由于格式问题,我没有看到您的所有代码(我自己也无法快速修补,因为您的q不可复制),但您可以使用override.aes。另一方面,你可以考虑组合数据来创建所有的段,创建更干净、更容易的代码。谢谢@ Axman,这与我公认的混乱代码很好。是的,通常最好提供一个仍然有相同问题的最小示例(即删除所有额外的位,如主题调用等)。而且,提供的(假)数据总是好的!是的,有很多不必要的信息。对此我很抱歉。显然,我对这一点非常陌生(基本上没有编码、R或堆栈方面的经验)。很高兴你能指出这一点。(顺便说一句,我甚至不知道如何提供数据。)
**Error in eval(expr, envir, enclos) : object 'Condition' not found**
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)
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))