如何避免由于R中的带状重复文本?

时间:2019-02-21 00:11:54

标签: r ggplot2

我想在R中的林图中添加文本,该文本中有带,但是在每个带上都重复该文本。如何仅将文本添加到一个条带上或仅在图上添加文本?我的代码如下:

我的数据就像:

     Group     Mean LowerLimit UpperLimit
       M      1.172827   1.083498   1.268857
       H      5.142589   4.333141   6.148088


   h<-"XXXX"

  p = ggplot(data=df4,
       aes(x = Group,y = Mean, ymin = LowerLimit, ymax = UpperLimit), 
      +  
       ggtitle(PlotTitle)+
       geom_point(aes(fill=Group, color=Group), shape=22, size=3)+
      geom_pointrange(aes(col=Group), fatten = 3)+
      geom_hline(aes(),yintercept =1, linetype="longdash")+
      geom_text(aes(-1.5, 0.8, vjust =-0.5, hjust=-0.8, size=10),label=h, 
      check_overlap = T)+
      geom_errorbar(aes(ymin=LowerLimit, 
      ymax=UpperLimit,col=Group),width=0.4,cex=1)+ 
      facet_wrap(~Group,strip.position="left",nrow=2, scales= "free_y") +
 theme(plot.title=element_text(aes(5, 5), hjust=0.5, size=14,face="bold"),
    legend.position='none',
    strip.text.y = element_text(size=10, hjust=0.5,vjust =1,lineheight=0.1, angle=270,face="bold"),
    panel.background = element_blank(),
    strip.background = element_rect(fill="green"),
    plot.margin = margin(3.5,0.1,3.5, 0.5, "cm"))+
    coord_flip()

 p

1 个答案:

答案 0 :(得分:0)

在您的geom_text参数中,尝试将label=h更改为label = ''

library(ggplot2)

df4 <- data.frame(Group = c("M", "H"),
                  Mean = c(1.172827, 5.142589),
                  LowerLimit = c(1.083498, 4.333141),
                  UpperLimit = c(1.268857,  6.148088))

PlotTitle = "Insert plot title here"

p = ggplot(data=df4,
           aes(x = Group,y = Mean, ymin = LowerLimit, ymax = UpperLimit)) + 
             ggtitle(PlotTitle) +
             geom_point(aes(fill=Group, color=Group), shape=22, size=3) +
             geom_pointrange(aes(col=Group), fatten = 3) +
             geom_hline(aes(),yintercept =1, linetype="longdash") +
             geom_text(aes(-1.5, 0.8, vjust =-0.5, hjust=-0.8, size=10),label='', 
                       check_overlap = T) +
             geom_errorbar(aes(ymin=LowerLimit, 
                               ymax=UpperLimit,col=Group),width=0.4,cex=1) + 
             facet_wrap(~Group,strip.position="left",nrow=2, scales= "free_y") +
             theme(plot.title=element_text(aes(5, 5), hjust=0.5, size=14,face="bold"),
                   legend.position='none',
                   strip.text.y = element_text(size=10, hjust=0.5,vjust =1,lineheight=0.1, angle=270,face="bold"),
                   panel.background = element_blank(),
                   strip.background = element_rect(fill="green"),
                   plot.margin = margin(3.5,0.1,3.5, 0.5, "cm")) +
             coord_flip()
p

产生此图像:

enter image description here