15

The standard means of removing gridlines seem futile when plotting with geom_sf.

For instance, if we plot a simple ggplot object, this works to remove the grid

library(tidyverse)
library(sf)

mtcars %>%
  ggplot(
    aes(disp, hp)
  ) +
  geom_point() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )

returns

scatterplot no grid

but the same code fails to remove the grid when you plot using geom_sf

"shape/nc.shp" %>% 
  system.file(
    package = "sf"
  ) %>% 
  st_read(
    quiet = TRUE
    ) %>%
  ggplot() +
  geom_sf(aes(fill = AREA)) +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
tomw
  • 3,114
  • 4
  • 29
  • 51

3 Answers3

27

This issue was raised on the ggplot2 github site. You can remove the gridlines by either of:

  1. Setting the colour of the gridlines to be transparent with theme(panel.grid.major = element_line(colour = "transparent"))

  2. Adding coord_sf(datum = NA) after calling geom_sf

Calum You
  • 14,687
  • 4
  • 23
  • 42
eipi10
  • 91,525
  • 24
  • 209
  • 285
1

Another way to remove the gridlines is to change the scale using scale_x_continuous:

library(ggplot2)
world <- rnaturalearth::ne_countries(scale = "medium", returnclass = "sf")

crs_robinson = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m no_defs"

ggplot() + 
  geom_sf(data = world, color="grey70", fill="grey70")  +
  theme(panel.border=element_blank(),
        panel.grid = element_line(colour = "grey70", size=2),
        axis.text.x= element_blank(),
        axis.text.y = element_blank()) + 
  labs(title = str_c("Map of without gridlines") ,
       x = "",
       y = "") +
  scale_x_continuous(breaks = c(-180, 180)) +
  scale_y_continuous(breaks=c(-89.999, 89.999)) +
  coord_sf(crs = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m no_defs", expand = TRUE)no_defs", expand = TRUE)

PS: Note that that for x scale_y_continuous(breaks=c(-90, 90)) gives an error.

enter image description here

Daniel
  • 21
  • 1
0

ggplot() + geom_sf(data = data, colour = NA)

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '22 at 19:24