Skip to content

Cannot remove panel.grid (graticules) from ggplot + geom_sf() #2071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tiernanmartin opened this issue Mar 10, 2017 · 7 comments
Closed

Cannot remove panel.grid (graticules) from ggplot + geom_sf() #2071

tiernanmartin opened this issue Mar 10, 2017 · 7 comments
Labels
bug an unexpected problem or unintended behavior themes 💃

Comments

@tiernanmartin
Copy link

I would like to completely remove the graticule lines from my plot, but it is unclear which argument in theme() controls the graticule lines.

Reprex

library(sf) 
library(ggplot2)

nc = st_read(system.file("gpkg/nc.gpkg", package = "sf")) 

ggplot(nc) + 
  geom_sf(data = nc, aes(fill = AREA)) + 
  coord_sf(crs = st_crs(nc)) + 
  theme(
    panel.ontop = TRUE,   ## Note: this is to make the panel grid visible in this example
    panel.grid = element_blank(), 
    line = element_blank(), 
    rect = element_blank(), 
    text = element_blank(), 
    plot.background = element_rect(fill = "black"))

@LucasPuente
Copy link

Looks like a bug.

One trick I discovered to solve this is to set the color of the panel.grid.major equal to the background color. In your example, this would mean + theme(panel.grid.major = element_line(colour = "black")).

I call this a bug because theme(panel.grid.major = element_blank()) is not working.

@mdsumner
Copy link

mdsumner commented Mar 17, 2017

Another use-case where it's important not to have the graticule is when the crs is NA. Currently there's a test for is.na(crs) in CoordSf that can fail, which could be replaced with

identical(sf::NA_crs_, crs)

I started a PR but can't test the outcome of this fix fully since the graticule logic doesn't work. An example is

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

nc_na <- sf::st_set_crs(nc, NA)
ggplot(nc_na) + coord_sf()
#Error in if (st_is_longlat(crs)) bb = trim_bb(bb, margin) : 
#  missing value where TRUE/FALSE needed

It's an error from sf but presume ggplot2 would like to be robust against this anyway (setting CRS to NA is required to avoid ellipsoid geometry logic in sf, and for any sf object that isn't some kind of map projection).

Happy to help explore more and create examples if that helps but the overall solution is beyond me atm.

@tiernanmartin
Copy link
Author

@LucasPuente or perhaps + theme(panel.grid.major = element_line(colour = 'transparent')) - this would allow the plot to be saved without any panel background.

@karawoo karawoo added bug an unexpected problem or unintended behavior themes 💃 labels Jun 12, 2017
edzer added a commit to r-spatial/sf that referenced this issue Jul 28, 2017
when giving coord_sf(..., datum = NA)
@edzer
Copy link
Contributor

edzer commented Jul 28, 2017

This requires coord_sf(datum = NA), e.g.

library(sf) 
library(ggplot2)

nc = st_read(system.file("gpkg/nc.gpkg", package = "sf")) 

ggplot(nc) + 
  geom_sf(data = nc, aes(fill = AREA)) + 
  coord_sf(crs = st_crs(nc), datum = NA) + 
  theme(
    panel.ontop = TRUE,   ## Note: this is to make the panel grid visible in this example
    panel.grid = element_blank(), 
    line = element_blank(), 
    rect = element_blank(), 
    text = element_blank(), 
    plot.background = element_rect(fill = "black"))

and still gives two warnings that need to be solved in ggplot2.
x

@edzer
Copy link
Contributor

edzer commented Jul 29, 2017

r-spatial/sf@812cdff now removes the need to patch ggplot2.

@jsta
Copy link
Contributor

jsta commented Jan 23, 2018

Note that the solution provided above will not hide the graticules if coord_sf is called before geom_sf.

@lock
Copy link

lock bot commented Jul 22, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior themes 💃
Projects
None yet
Development

No branches or pull requests

7 participants