0

I've used ggplot2 to plot some values on dates. By default, R chose 3 dates (always from January of several years) to label the x axis. So far so good.

I now want to "increase" the number of dates that are shown on the x axis but I have over 5000 observations so I can't do it manually. Do you have an idea of how to get like 6 dates or even better 6 certain dates shown on the x axes?

my.plot <-ggplot(data=data, aes(x=date, y=value, group=xyz, color=xyz)) +
  geom_line() +
  scale_x_date(date_labels = "%Y-%m")

Thank you very much in advance!

  • 1
    `scale_x_date(date_breaks = "4 months")` for example – NotThatKindODr Jan 06 '21 at 16:49
  • Thanks! That was super quick and easy to change. Now I am wondering whether I can insert sth like a "start" and "end" date for the labeling. – ostrich Jan 06 '21 at 17:00
  • I've tried it with xlim(2000-01,2020-01) but get error messages – ostrich Jan 06 '21 at 17:04
  • 2
    You are dealing with dates so you have to use proper dates. Additionally use the limits argument of scale_x_date, i.e. add e.g. `limits = c(as.Date("1950-01-01"), as.Date("2020-01-01"))` – stefan Jan 06 '21 at 17:16
  • Thanks! That worked in some way but not it is still not the way I would like to have it. Now I get the range and a certain number of dates but with date_breaks = "12 months" I get values from July instead of January. Sorry to ask again but is there a way to fix that too? – ostrich Jan 06 '21 at 17:50
  • Got it now. Just "cheated" by setting the start date 6 months earlier and then it starts on January first the year I wanted it to start : D – ostrich Jan 06 '21 at 18:08

0 Answers0