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

ggplot2's x-axis displays the starting date of next month instead of ending date of current month

Given a dataframe df:

df <- structure(list(date = structure(c(19204, 19235, 19265, 19296), class = "Date"), 
    value = c(3.5, 3.6, 4.05, 4.13)), row.names = c(NA, -4L), class = "data.frame")

Out:

       date value
1 2022-07-31  3.50
2 2022-08-31  3.60
3 2022-09-30  4.05
4 2022-10-31  4.13

I want the x-axis to display the date values of date column in df, that is, July, August, September, October, the last day of these months, but I have tried several methods, all of which are displayed on the first day of the next month. How could we deal with this problem? Thanks.

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

ggplot(data=df, aes(x=date, y=value)) +
  geom_line(linetype='solid', alpha = 1, size = 1.5, color='blue') +
  geom_point(alpha = 1, size = 4, color='blue') +
  # scale_x_date(limits = as.Date(c("2022-07-31", "2022-10-31"), format = '%Y-%m-%d'), breaks = '1 month')
  # scale_x_continuous(labels=format(as.Date(df$date), "%Y-%m-%d"))
  # scale_x_datetime(breaks = df$date, date_labels = '%Y-%m-%d') 
  # scale_x_date(breaks = "1 month", minor_breaks = "1 month", date_labels = "%Y-%m-%d")
  scale_x_date(breaks = "1 month", minor_breaks = "1 month", date_labels = "%Y-%m-%d",
               limits = c(min(df$date), max = max(df$date)))

Out:

enter image description here

>Solution :

If you provide an explicit list of breaks, ggplot will "obey". So, you could use the following code:

ggplot(data=df, aes(x=date, y=value)) +
  geom_line(linetype='solid', alpha = 1, size = 1.5, color='blue') +
  geom_point(alpha = 1, size = 4, color='blue') + 
  scale_x_date(breaks = as.Date(c("2022-07-31", "2022-08-31", "2022-09-30", "2022-10-31")))

or even shorter:

ggplot(data=df, aes(x=date, y=value)) +
  geom_line(linetype='solid', alpha = 1, size = 1.5, color='blue') +
  geom_point(alpha = 1, size = 4, color='blue') + 
  scale_x_date(breaks = df$date)

yielding this plot:

enter image description here

The solution seems trivial, so I have to ask: Is this what you are after?

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