Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plotting dates on the x-axis and customizing labels

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()

Plot1

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Plot2

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")
  )

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading