简体   繁体   中英

How to avoid repeated text due to strip in R?

I want to add a text to my forest plot in R that has strip in it but the text is repeated on every strip . how can I add only the text to one strip or just on the plot? My code is as below:

My data is like:

     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

In your parameters for geom_text try changing label=h to 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

which yields this image:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM