Is there a way to force an automatic line break in the ggtitle
command?
As part of a shiny application I use different plots with an input variable for the title. Unfortunately, some of these variable values are too long to be displayed.
I came across one possibility by inserting \n
(or a line break) manually in advance (Details here: Improve editing of multiline title in ggplot that uses \n and extends far to the right). There, something like ggtitle(paste("", title, "", sep = "\n"))
would be suggested.
Since including \n
would be error prone as well as not flexible for different plot sizes I´m looking for another possibility.
Here is an minimal working example (adapted from lawyeR question linked above):
DF <- data.frame(x = rnorm(400))
title_example <- "This is a very long title describing the plot in its details. The title should be fitted to a graph, which is itself not restricted by its size."
plot <- ggplot(DF, aes(x = x)) + geom_histogram() +
ggtitle(title_example)
You can find a image here: https://i.stack.imgur.com/6MMKF.jpg
Do you have an idea how to e.g. adapt the sep =
operator to break lines automatically or are there maybe packages/ workarounds? Thanks!