Skip to content

Commit 7389a3f

Browse files
authored
Better respect theme settings when drawing coord_sf() panel grid (#3113)
* Make coord_sf() panel grid mimic coord_cartesian() panel grid. Closes #2991. Closes #2525. * don't drop gray background * add unit test * don't error if line size is undefined
1 parent 18be30e commit 7389a3f

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# ggplot2 3.1.0.9000
22

3+
* `coord_sf()` graticule lines are now drawn in the same thickness as
4+
panel grid lines in `coord_cartesian()`, and seting panel grid
5+
lines to `element_blank()` now also works in `coord_sf()`
6+
(@clauswilke, #2991, #2525).
7+
38
* `geom_hline()`, `geom_vline()`, and `geom_abline()` now throw a warning if the user supplies both an `xintercept`, `yintercept`, or `slope` value and a mapping (@RichardJActon, #2950).
49

510
* `scale_color_continuous()` now points at `scale_colour_continuos()` so that it

R/coord-sf.R

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,22 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
195195

196196
render_bg = function(self, panel_params, theme) {
197197
el <- calc_element("panel.grid.major", theme)
198-
line_gp <- gpar(col = el$colour, lwd = el$size, lty = el$linetype)
199-
grobs <- c(
200-
list(element_render(theme, "panel.background")),
201-
lapply(sf::st_geometry(panel_params$graticule), sf::st_as_grob, gp = line_gp)
202-
)
198+
199+
# we don't draw the graticules if the major panel grid is
200+
# turned off
201+
if (inherits(el, "element_blank")) {
202+
grobs <- list(element_render(theme, "panel.background"))
203+
} else {
204+
line_gp <- gpar(
205+
col = el$colour,
206+
lwd = len0_null(el$size*.pt),
207+
lty = el$linetype
208+
)
209+
grobs <- c(
210+
list(element_render(theme, "panel.background")),
211+
lapply(sf::st_geometry(panel_params$graticule), sf::st_as_grob, gp = line_gp)
212+
)
213+
}
203214
ggname("grill", do.call("grobTree", grobs))
204215
},
205216

tests/figs/coord-sf/no-panel-grid.svg

Lines changed: 63 additions & 0 deletions
Loading

tests/testthat/test-coord_sf.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
context("coord_sf")
22

3-
test_that("multiplication works", {
3+
test_that("basic plot builds without error", {
44
skip_if_not_installed("sf")
55

66
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
@@ -15,6 +15,18 @@ test_that("multiplication works", {
1515
expect_doppelganger("sf-polygons", plot)
1616
})
1717

18+
test_that("graticule lines can be removed via theme", {
19+
skip_if_not_installed("sf")
20+
21+
df <- data_frame(x = c(1, 2, 3), y = c(1, 2, 3))
22+
plot <- ggplot(df, aes(x, y)) +
23+
geom_point() +
24+
coord_sf() +
25+
theme_gray() + # to test for presence of background grob
26+
theme(panel.grid = element_blank())
27+
28+
expect_doppelganger("no panel grid", plot)
29+
})
1830

1931
test_that("axis labels can be set manually", {
2032
skip_if_not_installed("sf")

0 commit comments

Comments
 (0)