rggplot2r-sftigris

coord_sf forces ggplot grid lines?


I'm using ggplot2 and the sf and tigris packages to draw some maps (with geom_sf()). I was finding that I couldn't turn off grid lines, despite calling theme(panel.grid = element_blank()) and it seems like it's due to using coord_sf.

Here's a non-map example, that's a simpler way to reproduce my issue

library(ggplot2)

dat <- data.frame(x=rnorm(10),
                  y=rnorm(10))

# grid lines, as expected
ggplot(dat, aes(x,y)) +
  geom_point() +
  theme_light()

enter image description here

# no grid lines, as expected
ggplot(dat, aes(x,y)) +
  geom_point() +
  theme_light() +
  theme(panel.grid = element_blank())

enter image description here

# why does this have grid lines?
ggplot(dat, aes(x,y)) +
  geom_point() +
  coord_sf() +
  theme_light() +
  theme(panel.grid = element_blank())

enter image description here

I'd like to use coord_sf but also turn off gridlines.


Solution

  • As markus linked, https://github.com/tidyverse/ggplot2/issues/2071, this is a bug, adding datum=NA to coord_sf() fixes:

    ggplot(dat, aes(x,y)) +
      geom_point() +
      coord_sf(datum=NA) +
      theme_light() +
      theme(panel.grid = element_blank())