0

Given this example dataset:

date <- seq(as.Date("2019-01-01"), as.Date("2019-12-31"), "days")
Y <- rnorm(length(date),0,1)
data.frame(date, Y) %>% ggplot(aes(y = Y, x = date)) + geom_point()

I get the following default plot: enter image description here

The vertical lines appear on every 3rd month (1st day), with one in betweeen. I would like these lines to appear on every (1st day of) month.

Is there a way to do this automatically (without specifying the actual dates) - for instance can I tell it that I want 13 lines in total (1 for each month) ? Or any other ways would be interesting.

EDIT: The linked answers for duplicates do not address this situation. They create vertical lines for months AND corresponding labels. I am happy with the labels every 3 months - I just want the vertical lines to be every month

Henrik
  • 65,555
  • 14
  • 143
  • 159
Robert Long
  • 5,722
  • 5
  • 29
  • 50
  • 2
    I think you might be looking for `scale_x_date(date_breaks = "1 month")` – teunbrand Jul 28 '20 at 10:37
  • 1
    Or to keep the labelling consistent `scale_x_date(breaks = "1 month", labels = scales::date_format("%b %Y"))` – Allan Cameron Jul 28 '20 at 10:42
  • @teunbrand thanks, but that's not quite what I want because then I get axis labels for every month too - I like the labels every 3 months, if they are every month then it is too cluttered – Robert Long Jul 28 '20 at 10:47
  • Then I think you need to create a `labels` variable as described e.g. here: [ggplot plot axis ticks and labels separately](https://stackoverflow.com/questions/28263721/ggplot-plot-axis-ticks-and-labels-separately); I assume it can be done more programmatically. – Henrik Jul 28 '20 at 11:03
  • See e.g. here: [ggplot2 displaying unlabeled tick marks between labeled tick marks](https://stackoverflow.com/questions/46412250/ggplot2-displaying-unlabeled-tick-marks-between-labeled-tick-marks), where modulo (`%%`) is used to determine positions where the (default) labels should be replaced with 'blank' (`""`). Also: [Adding minor tick marks to the x axis in ggplot2 (with no labels)](https://stackoverflow.com/questions/14490071/adding-minor-tick-marks-to-the-x-axis-in-ggplot2-with-no-labels) – Henrik Jul 28 '20 at 11:13
  • And [Insert blanks into a vector for, e.g., minor tick labels in R](https://stackoverflow.com/questions/34533472/insert-blanks-into-a-vector-for-e-g-minor-tick-labels-in-r) – Henrik Jul 28 '20 at 11:16

1 Answers1

0

What you're calling tick marks are called breaks in ggplot2 jargon. The ones with labels are major breaks, the ones without are minor breaks. So you can probably get what you want using this code:

date <- seq(as.Date("2019-01-01"), as.Date("2019-12-31"), "days")
Y <- rnorm(length(date),0,1)
data.frame(date, Y) %>% ggplot(aes(y = Y, x = date)) + 
  geom_point() + scale_x_date(date_minor_breaks = "1 month")

One possible issue is that minor breaks are half the width of major breaks. If you want the one month breaks to be all the same size, you need to change the theme. For example,

date <- seq(as.Date("2019-01-01"), as.Date("2019-12-31"), "days")
Y <- rnorm(length(date),0,1)
data.frame(date, Y) %>% ggplot(aes(y = Y, x = date)) + 
  geom_point() + scale_x_date(date_minor_breaks = "1 month") +
  theme(panel.grid.minor.x = element_line(size = rel(2)))

I used rel(2) to undo the rel(0.5) for "panel.grid.minor". This leaves the minor breaks at half width for Y, but sets all breaks to the same size for date:

screenshot

user2554330
  • 37,248
  • 4
  • 43
  • 90