Add dynamic subtitle using ggplot

后端 未结 1 874
花落未央
花落未央 2020-12-07 23:32

I am trying to use ggplot to add a subtitle. Similar question was asked here: How to add a ggplot2 subtitle with different size and colour?, and the answer was as follows:<

相关标签:
1条回答
  • 2020-12-07 23:50

    You should use function bquote() instead of expression() to use titles that are stored as variables. And variable names should be placed inside .()

    plot.title = 'TITLE'
    plot.subtitle = 'SUBTITLE'
    
    ggplot(mtcars,aes(disp,mpg))+geom_point()+
      ggtitle(bquote(atop(.(plot.title), atop(italic(.(plot.subtitle)), "")))) 
    

    enter image description here

    UPDATE - ggplot2 version 2.2.1

    The latest ggplot2 version now can produce subtitles directly, so you don't have to use bquote() and expression(). The result is atchieved with argument subtitle = of function labs().

    ggplot(mtcars,aes(disp,mpg))+geom_point()+
          labs(title = plot.title,subtitle = plot.subtitle) +
          theme(plot.subtitle = element_text(face = "italic"))
    
    0 讨论(0)
提交回复
热议问题