rggplot2r-marquee

When using the marquee package for a ggplot in R, how do I control line breaks and line spacing in a title?


I have a very long title in a ggplot. I am using the marquee package for formatting parts of it. Why do I need to use two \n to get a line break? How do I control the spacing between lines? I would like to have very little space between the lines of the title.

Here is an example that shows how a single \n is not being applied and my failed attempts to control the line spacing:

library(marquee)
library(ggplot2)

my_style <- modify_style(
  classic_style(),
  "my_style",
  lineheight = relative(0.5),
  margin = trbl(0, 0, 0)
)

ggplot() +
  labs(title = "A good figure title is long because:\n it includes deatils on what was studdied,when the data\n\n were collected, and where. For example, this is a bunch of data on fuel economy for cars\n\ngathered from a small planet which some call Earth between the years 1999 and 2008.") +
  theme(title = element_marquee(style = my_style))

enter image description here

If there is a simple way to wrap title text when using {marquee} I would love to know it.


Solution

  • According to the {marquee} syntax vignette you can achieve your desired result by adding two trailing space before the line break:

    If you want to force a linebreak inside your paragraph, you end the line with two spaces and a linebreak.

    library(marquee)
    library(ggplot2)
    
    my_style <- modify_style(
      classic_style(),
      "my_style",
      lineheight = relative(0.5),
      margin = trbl(0, 0, 0)
    )
    
    ggplot() +
      labs(title = "A good figure title is long because:  \nit includes details on what was studdied,when the data\n\n were collected, and where. For example, this is a bunch of data on fuel economy for cars\n\ngathered from a small planet which some call Earth between the years 1999 and 2008.") +
      theme(title = element_marquee(style = my_style))
    

    enter image description here