R: ggplot2, can I set the plot title to wrap around and shrink the text to fit the plot?

前端 未结 3 1900
梦如初夏
梦如初夏 2020-11-30 06:42
library(ggplot2)

my_title = \"This is a really long title of a plot that I want to nicely wrap \\n and fit onto the plot without having to manually add the backslas         


        
3条回答
  •  醉酒成梦
    2020-11-30 07:09

    You have to manually choose the number of characters to wrap at, but the combination of strwrap and paste will do what you want.

    wrapper <- function(x, ...) 
    {
      paste(strwrap(x, ...), collapse = "\n")
    }
    
    my_title <- "This is a really long title of a plot that I want to nicely wrap and fit onto the plot without having to manually add the backslash n, but at the moment it does not"
    r + 
      geom_smooth() + 
      ggtitle(wrapper(my_title, width = 20))
    

提交回复
热议问题