Skip to content

allow geom_sf to accept line parameters (#2826) #2847

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

Merged
merged 11 commits into from
Aug 23, 2018
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 3.0.0.9000

* `geom_sf()` now respects `lineend`, `linejoin`, and `linemitre` parameters
for lines and polygons (@alistaire47, #2826)

* `benchplot()` now uses tidy evaluation (@dpseidel, #2699).

* `fortify()` now displays a more informative error message for
Expand Down
16 changes: 12 additions & 4 deletions R/sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,21 @@ GeomSf <- ggproto("GeomSf", Geom,
stroke = 0.5
),

draw_panel = function(data, panel_params, coord, legend = NULL) {
draw_panel = function(data, panel_params, coord, legend = NULL,
lineend = "butt", linejoin = "round", linemitre = 10) {
if (!inherits(coord, "CoordSf")) {
stop("geom_sf() must be used with coord_sf()", call. = FALSE)
}

# Need to refactor this to generate one grob per geometry type
coord <- coord$transform(data, panel_params)
grobs <- lapply(1:nrow(data), function(i) {
sf_grob(coord[i, , drop = FALSE])
sf_grob(
coord[i, , drop = FALSE],
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
)
})
do.call("gList", grobs)
},
Expand All @@ -186,7 +192,7 @@ default_aesthetics <- function(type) {
}
}

sf_grob <- function(row) {
sf_grob <- function(row, lineend, linejoin, linemitre) {
# Need to extract geometry out of corresponding list column
geometry <- row$geometry[[1]]

Expand All @@ -207,7 +213,9 @@ sf_grob <- function(row) {
fill = alpha(row$fill, row$alpha),
lwd = row$size * .pt,
lty = row$linetype,
lineend = "butt"
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
)
sf::st_as_grob(geometry, gp = gp)
}
Expand Down