Plotting dates on the x-axis and customizing labels

Advertisements

I am trying to make a graph of a time series, but I’m having trouble achieving my desired result.
I have data from a time series from May 2021 to December 2022. Original data is by day, but for my visualization, months will suffice. I used lubridate::floor_date() and summarised the observations. The resulting data is like this:

df <- data.frame(Date = c("2021-05-01","2021-06-01","2021-07-01","2021-08-01","2021-09-01","2021-10-01","2021-11-01","2021-12-01","2022-01-01","2022-02-01","2022-03-01","2022-04-01","2022-05-01","2022-06-01","2022-07-01","2022-08-01","2022-09-01","2022-10-01","2022-11-01","2022-12-01"),
                 n = c(30,44,58,49,52,40,31,21,29,17,25,14,20,17,10,11,7,20,5,4)) %>%
  dplyr::mutate(Date = lubridate::ymd(Date))

ggplot2::ggplot(data=df,
       aes(x=Date,
           y=n)) +
  geom_line()

The resulting plot is alright, and I like that it omits the days by default. However, I want the x-axis tick labels to start at 2021-05 and end at 2022-12, so I looked into scale_x_continuous and thought I’d play around, starting from displaying 20 ticks (one for each month in my time series) with n.breaks = 20 and go from there. The result somehow botched the labels, so I am out of ideas.

Eventually, I would like to assign custom labels using the labels argument inside scale_x_continuous to have my labels say "May 2021" or "December 2022".

Thank you all in advance!

>Solution :

One option would be to set your desired breaks via the breaks argument . In the code below I used a sequence by 3 month intervals but you could set them to your liking. Also I formatted the dates to your desired format using the labels argument:

library(ggplot2)

ggplot(
  data = df,
  aes(
    x = Date,
    y = n
  )
) +
  geom_line() +
  scale_x_continuous(
    breaks = seq.Date(as.Date("2021-05-01"), as.Date("2022-12-01"), by = "3 month"),
    labels = ~ format(.x, "%b %Y")
  )

Leave a ReplyCancel reply