Left-adjust title in ggplot2, or absolute position for ggtitle

前端 未结 5 1725
广开言路
广开言路 2020-12-02 23:16

I would like to left align the title in a plot like this

ggplot(data = economics, aes(x = date, y = unemploy)) +
 geom_line() +
 ggtitle(\"Unemployment in US         


        
5条回答
  •  时光说笑
    2020-12-02 23:41

    This question refers to this github tidyverse/ggplot2 solved issue: https://github.com/tidyverse/ggplot2/issues/3252

    And it is implemented in ggplot2 (development version): https://github.com/tidyverse/ggplot2/blob/15263f7580d6b5100989f7c1da5d2f5255e480f9/NEWS.md

    Themes have gained two new parameters, plot.title.position and plot.caption.position, that can be used to customize how plot title/subtitle and plot caption are positioned relative to the overall plot (@clauswilke, #3252).

    To follow your example as a reprex:

    # First install the development version from GitHub:
    #install.packages("devtools") #If required
    #devtools::install_github("tidyverse/ggplot2")
    
    library(ggplot2)
    packageVersion("ggplot2")
    #> [1] '3.2.1.9000'
    
    ggplot(data = economics, aes(x = date, y = unemploy)) + 
      geom_line() +
      labs(x=NULL,
           y="Unemployed [thousands]",
           title = "Unemployment in USA for some years",
           subtitle = "A subtitle possibly",
           caption  = "NOTE: Maybe a caption too in italics.") +
      theme(plot.caption = element_text(hjust = 0, face= "italic"), #Default is hjust=1
            plot.title.position = "plot", #NEW parameter. Apply for subtitle too.
            plot.caption.position =  "plot") #NEW parameter
    

    Created on 2019-09-04 by the reprex package (v0.3.0)

提交回复
热议问题