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

前端 未结 5 1738
广开言路
广开言路 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:25

    I wrote the ggdraw() layer in cowplot specifically so that I could make annotations easily anywhere on a plot. It sets up a coordinate system that covers the entire plot area, not just the plot panel, and runs from 0 to 1 in both the x and y direction. Using this approach, it is easy to place your title wherever you want.

    library(cowplot)
    theme_set(theme_gray()) # revert to ggplot2 default theme
    
    p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
      geom_line() +
      ggtitle("") + # make space for title on the plot
      xlab("") +
      ylab("Unemployed [thousands]")
    
    ggdraw(p) + draw_text("Unemployment in USA between 1967 and 2007", 
                          x = 0.01, y = 0.98, hjust = 0, vjust = 1,
                          size = 12)  # default font size is 14, 
                                      # which is too big for theme_gray()
    

提交回复
热议问题