3

I really like the aesthetics of The Economist magazine and I use the theme_economist often. However, I am curious as to how they create the red lines in the top left in a lot of their charts. See image below and where I circled.

enter image description here

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Jaskeil
  • 1,044
  • 12
  • 33
  • 1
    does the red line have a meaning? (This doesn't help with your question, but I'm curious) – Ben Bolker Nov 03 '20 at 05:19
  • no I just thinks it looks nice and professional – Jaskeil Nov 03 '20 at 13:10
  • 1
    It would help if you could provide a simple reproducible example of a graph to which you'd like to add the red line. Also, I don't see anything circled in the image you provided. – Claus Wilke Nov 03 '20 at 15:13
  • yeah sorry forgot to add that piece, I am just talking about that red line on the top left of the chart – Jaskeil Nov 03 '20 at 15:38

1 Answers1

2

This question is a mix of "how to annotate outside the plot area" and "how to annotate in npc coordinates". Therefore, I offer two options.

Both unfortunately require a bit of trial and error in order to correctly place the segment. For option 1, it is the y coordinate which we have to "guess", and for option 2 it's x!

In order to make y slightly less guess work, I tried an approach to position is relative to the default axis breaks. using the fabulous information from this answer. This is of course not necessary, one can also simply trial and error.

For option 2, I modified a function from user Allan Cameron's answer here. He mentions a way to figure out x and y, I guess one could use the title, and then place the annotation based on that.

library(ggplot2)

p <-
  ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  ggtitle("lorem ipsum") +
  theme(plot.margin = margin(t = 1.5, unit = "lines")) # this is always necessary

# OPTION 1
# semi-programmatic approach to figure out y coordinates
y_defaultticks <- with(mtcars, labeling::extended(range(wt)[1], range(wt)[2], m = 5))
y_default <- y_defaultticks[2] - y_defaultticks[1] 
y_seg <- max(mtcars$wt) + 0.75 * y_default

p +
annotate(geom = "segment", x = - Inf, xend = 12, y = y_seg, yend = y_seg, 
         color = "red", size = 5) +
  coord_cartesian(clip = "off", ylim = c(NA, max(mtcars$wt)),
                  xlim = c(min(mtcars$mpg), NA)) 



# OPTION 2
annotate_npc <- function(x, y, height, width, ...) {
  grid::grid.draw(grid::rectGrob(
    x = unit(x, "npc"), y = unit(y, "npc"), height = unit(height, "npc"), width = unit(width, "npc"),
    gp = grid::gpar(...)
  ))
}

p
annotate_npc(x = 0.07, y = 1, height = 0.05, width = 0.05, fill = "red", col = NA)

Created on 2021-01-02 by the reprex package (v0.3.0)

tjebo
  • 21,977
  • 7
  • 58
  • 94