Skip to content

coord_sf() doesn't adjust line size for graticule lines #2991

Closed
@clauswilke

Description

@clauswilke

Line sizes in themes are generally provide in units of mm and converted to grid units by multiplication by .pt. However, coord_sf() doesn't do that, it uses the value of size directly. Reprex:

library(ggplot2)

df <- data.frame(x = c(1, 2, 3), y = c(1, 2, 3))
p <- ggplot(df, aes(x, y)) + geom_point()

cowplot::plot_grid(
  p + coord_fixed() + theme(panel.grid.minor = element_blank()) +
    ggtitle("coord_fixed()"),
  p + coord_sf() +
    ggtitle("coord_sf() default"),
  p + coord_sf() + theme(panel.grid = element_line(size = 0.5*.pt)) +
    ggtitle("coord_sf() theme adjusted"),
  nrow = 2
)

Created on 2018-11-09 by the reprex package (v0.2.1)

The offending lines are here:

ggplot2/R/sf.R

Lines 576 to 582 in 6786969

render_bg = function(self, panel_params, theme) {
el <- calc_element("panel.grid.major", theme)
line_gp <- gpar(col = el$colour, lwd = el$size, lty = el$linetype)
grobs <- c(
list(element_render(theme, "panel.background")),
lapply(sf::st_geometry(panel_params$graticule), sf::st_as_grob, gp = line_gp)
)

The fix is simple, but it would affect the visual appearance of a lot of plots out in the wild.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions