4

I created a graph plotting data by day of the year with an x-axis where the tick marks indicate the start of each month and the minor grid lines indicate the start of each week.

I intentionally don't show the major grid lines on the x-axis, which would make the graph too busy. However, this has an unexpected effect: the minor grid lines don't show when they coincide with a (disabled) major grid line. (That is, whenever the first day of the month falls on a Monday, here, Jan 1st and Oct 1st 2018.)

Could this be a bug, and is there a way to work around this issue?

library(tidyverse)
library(lubridate)

dat = tibble(
  date = as_date(c("2018-01-01", "2019-01-01")),
  proportion = c(.2, .8)
)

dat %>% 
  ggplot(aes(x = date, y = proportion)) +
  geom_point() +
  scale_x_date(breaks = "1 month", minor_breaks = "1 week", date_labels = "%b") +
  scale_y_continuous(limits = c(0, 1)) +
  theme(
    panel.grid.major.x = element_blank(),
    panel.grid.minor.y = element_blank()
  )

graph

user2363777
  • 947
  • 8
  • 18
  • `panel.grid.major.x = element_blank` removes major grid lines along the x-axis, which is something that I want to do in this graph. – user2363777 Aug 17 '19 at 00:57

1 Answers1

2

Actually, this is intended behaviour. See discussion under this (really old) issue on GitHub, which resulted in ggplot2:::guide_grid (an un-exported function that manipulates the gridlines before a ggplot object is printed) keeping only minor gridlines that don't overlap with major ones:

> ggplot2:::guide_grid
function (theme, x.minor, x.major, y.minor, y.major) {
  x.minor <- setdiff(x.minor, x.major)
  y.minor <- setdiff(y.minor, y.major)
  ...
}

Workaround

One quick & dirty way to get around this is to run trace(ggplot2:::guide_grid, edit=TRUE) beforehand, & manually remove the x.minor <- setdiff(x.minor, x.major) line from code in the popup window.

Your code should work as expected thereafter:

plot

(This effect will end when you close your R session, or earlier if you run untrace(ggplot2:::guide_grid)).

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Thanks for the workaround! I'm not sure the github discussion is relevant, since it focuses on avoiding drawing minor grid lines underneath major grid lines. In my graph, there are no major grid lines. But the absence of a major grid line should not affect the drawing of a minor grid line at the same location. – user2363777 Aug 17 '19 at 12:59
  • The major gridlines aren't *displayed* due to the theme specification, but they do exist in the plot's layout. Hence when `ggplot2:::guide_grid` decides which gridline positions require grob creation (an earlier step in the plot drawing process, before theme elements are taken into account), it handles the overlap between major vs. minor based on all the gridlines' positions. – Z.Lin Aug 17 '19 at 13:24
  • @Z.Lin Thank you for providing this solution! Do you maybe have an idea about what would be a reproducilble way to do this, since the part with manual editing in a separate box doesn't get saved anywhere? – J. Doe Sep 30 '22 at 19:31