1

I want to display a relatively long tag and do not want this tag to consume space for the chart.

Example:

library(ggplot2)
out <- ggplot(economics, aes(x=date,y=unemploy)) + 
              geom_line() + 
              labs(title="US unemployment rate", 
                   subtitle="(%)",
                   caption="Source: St. Louis Fed.\n Last observation: April 2014.",
                   tag="us_unempl.pdf (last update: 2019-01-15, 22:30)") +
              theme(plot.caption=element_text(hjust=0),
                    plot.tag=element_text(size=rel(1)),
                    plot.tag.position="bottomright")
print(out)

In this example, the relatively wide tag results in a substantial loss of space for the actual chart, because the chart's right margin is moving to the left.

How can I display the tag below the chart - ideally just opposite of the caption's second line or, alternatively, aligned to the right margin of the plot below the caption?

Note: I need the caption for other information (such as in my example), otherwise it would obviously be a natural solution to use hjust=1 in plot.caption.

pogibas
  • 27,303
  • 19
  • 84
  • 117
Ferdinand
  • 25
  • 5

1 Answers1

6

You can manually specify tag position using numeric vector with x and y positions (plot.tag.position). c(x, y) should be between 0 and 1. c(0, 0) puts tag to “bottom left” and c(1, 1) puts tag to “top right”.

library(ggplot2)
ggplot(mtcars, aes(cyl, mpg)) + 
    geom_line() + 
    labs(title = "US unemployment rate", 
         subtitle = "(%)",
         caption = "Source: St. Louis Fed.\n Last observation: April 2014.",
         tag = "us_unempl.pdf (last update: 2019-01-15, 22:30)") +
    theme(plot.caption = element_text(hjust = 0),
          plot.tag = element_text(size = rel(1)),
          plot.tag.position = c(0.85, 0.05))

enter image description here

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • Thank you. Combined with plot.tag=element_text(hjust=1) and a plot.tag.position = c(1,0.01), this is exactly what I need. Perfect! – Ferdinand Jan 16 '19 at 08:11