Home > front end >  Asterisk next to an x-axis label, ggplot2
Asterisk next to an x-axis label, ggplot2

Time:10-06

I am creating a bar chart in ggplot2, with every year from 2010-2022 on the x axis.

Year <- c(2010:2022)

However, I would like an asterisk by 2022, so I can make a note in the caption saying that the figures for this year are incomplete. I am not sure how to do this. I have tried Year <- c(2010:2021, "2022*") but that obviously did not work.

Any help would be much appreciated.

CodePudding user response:

One option would be to pass a small custom function to the labels argument of the scale which using an ifelse and paste0 adds an asterix to the label for year 2022.

Using some fake example data:

library(ggplot2)

dat <- data.frame(
  Year = 2010:2022,
  value = seq(13)
)

ggplot(dat, aes(Year, value))  
  geom_col()  
  scale_x_continuous(
    breaks = dat$Year,
    labels = ~ ifelse(.x == 2022, paste0(.x, "*"), .x)
  )

  • Related