Home > Software engineering >  How many I make a long caption on a geom_boxplot appear?
How many I make a long caption on a geom_boxplot appear?

Time:03-01

I have a long caption that I would like to include at the bottom of a ggplot geom_boxplot.

Currently the code writes a single line to the caption with lots of the intended caption missing. How might I wrap the text in the caption?

My code is

p = ggplot(mpg, aes(class, hwy))   geom_boxplot()  
  labs(title="An example title", caption = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")

I have experimented with a few things but have got nowhere e.g. plot.caption in the theme of the ggplot. Could anyone help?

Thank you. Phil,

CodePudding user response:

  1. Manually add \n to the place where you would like to break the text to a new line (this method is not used in this solution). However, since your caption is quite long, it's hard to guess where is the best to put the \n, and not easy to align the resulting text, therefore I recommend the second method:
  2. Use a function, such as stringr::str_wrap().
library(ggplot2)
library(stringr)

ggplot(mpg, aes(class, hwy))   geom_boxplot()  
  labs(title="An example title", caption = str_wrap("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", width = 80))

str_wrap_width80

CodePudding user response:

Another option and alternative to str_wrap would be ggtext::element_textbox. A benefit of ggtext::element_textbox is that you don't have to set the linewidth, instead you specify the width of the textbook, e.g. in my example code below I use unit(.8, "npc") so that the caption textbox will have an length of 80 percent of the width of plot.

library(ggplot2)
library(ggtext)

ggplot(mpg, aes(class, hwy))   
  geom_boxplot()  
  theme(plot.caption = ggtext::element_textbox(width = unit(.8, "npc"), hjust = 1, vjust = 1))  
  labs(title="An example title", caption = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")

  • Related