How to customize an individual axis tick

How do I format the position of an axis tick like the '60 yrs' in the attached picture? I can't find anything that helps.

I suspect that the "yrs" is an annotation (e.g. via https://ggplot2.tidyverse.org/reference/annotate.html), rather than an axis tick of anything like that, but I could be completely wrong.

When I try and write it in as an annotation, it can only go as far left as the lowest X value in the data set, which is not far enough over to be continuous with the axis tick label

Good point.

You are unlikely to be able to recreate many 538 plots straight out of the box. They apply a lot of customisation and I believe that they keep the code private.

You could play around with gridExtra::grid.text() or ggpubr::text_grob() or similar.

If you can't find a better solution, you could manually modify the scale like in this example.
Note: Please make your questions providing a REPRoducible EXample (reprex) like the one below.

library(tidyverse)
set.seed(1)
df <- data.frame(year = seq(1950, 2010, 10),
                 value = rnorm(7))

df %>% 
    ggplot(aes(year, value)) +
    geom_line() +
    scale_x_continuous(breaks = seq(1950, 2010, 10),
                       labels =str_replace(as.character(df$year), "^\\d{2}(?!(5|0))", "'")) +
    theme_minimal()

Created on 2019-09-12 by the reprex package (v0.3.0.9000)

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.