Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 backslash n, but at the moment it does not"  r <- ggplot(data = cars, aes(x = speed, y = dist)) r + geom_smooth() + #(left)  opts(title = my_title) 

can I set the plot title to wrap around and shrink the text to fit the plot?

like image 394
John Avatar asked Apr 13 '10 17:04

John


People also ask

How do you wrap a title in ggplot2?

Use 'wrap_labs()' to wrap the title, subtitle, and caption of a ggplot2 chart onto multiple lines, left-align them, and split 'notes' and 'source' onto multiple lines.

How do I change the size of my Ggtitle?

To change the size of the title and subtitle, we add the theme() function to labs() or ggtitle() function, whatever you used. Here we use labs() function. Inside theme() function, we use plot. title parameter for doing changes in the title of plot and plot.


2 Answers

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)) 
like image 150
Richie Cotton Avatar answered Sep 23 '22 10:09

Richie Cotton


Just for an update as has been mentioned in the comments opts is deprecated. You need to use labs and you could do:

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 backslash n, but at the moment it does not" 

Option 1: Using str_wrap option from the stringr package and setting your ideal width:

 library(stringr)  ggplot(data = cars, aes(x = speed, y = dist)) +       geom_smooth() +       labs(title = str_wrap(my_title, 60)) 

Option 2: Using the function provided by @Richie https://stackoverflow.com/a/3935429/4767610 like this:

wrapper <- function(x, ...)  {   paste(strwrap(x, ...), collapse = "\n") } ggplot(data = cars, aes(x = speed, y = dist)) +       geom_smooth() +       labs(title = wrapper(my_title, 60)) 

Option 3: Using the manual option (granted, this is what the OP wanted to avoid but it might be handy)

my_title_manual = "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 \n the backslash n, but at the moment it does not"   ggplot(data = cars, aes(x = speed, y = dist)) +           geom_smooth() +           labs(title = my_title_manual) 

Option 4: Reduce the text size of the title (as in the accepted answer https://stackoverflow.com/a/2633773/4767610)

ggplot(data = cars, aes(x = speed, y = dist)) +   geom_smooth() +   labs(title = my_title) +   theme(plot.title = element_text(size = 10)) 
like image 37
User2321 Avatar answered Sep 24 '22 10:09

User2321