monthly ticks on x axis


How can I add monthly ticks to a plot? I tried adding the following function to my plot

scale_x_date(date_breaks="1 month", date_labels="%m-%Y")

but it doesn't work. How can i properly use this function?


It is quite possible that your intended 'dates' are not formatted as dates.
This is a very common reason for this sort of failure.
Check your data types with str(data) or glimpse(data).

It would be more helpful if you could provide a reprex. Click the link and follow the instructions

This is an example of a reprex:

#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>     date, intersect, setdiff, union

data <- tribble( ~date, ~ value,
         '2020-01-01', 12,
         '2020-02-01', 16,
         '2020-03-01', 21,
         '2020-04-01', 27,
         '2020-05-01', 35) %>% 
  mutate(date = ymd(date))

ggplot(data) +
  aes(x = date, y = value) +
  geom_point( ) +
  scale_x_date(date_breaks="1 month", date_labels="%B-%Y")

Created on 2020-06-08 by the reprex package (v0.3.0)


thank you for your help. I tried to add it but it doesn't work. The code for my plot looks like this. My data contains monthly wages. I'm not sure where I can add it or if its the right function as i'm not using ggplot.

     ylim=c(1500,4500), ylab="Wage",
     xlim=as.Date(c("2019-01-31", "2020-05-31")))

thank you

On one hand, this might be an excellent time to convert to ggplot and the tidyverse. You will find that the packages are quite consistent, and lubridate is especially helpful for dates.

If you are determined to stick to base R, then Stack overflow is probably a better place to find an answer. Take a look at this post for a start:

Thank you fo the tip!

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