Skip to content

Commit cad7f78

Browse files
authored
Add arrow to line params (#4659)
1 parent ad3dbfc commit cad7f78

File tree

5 files changed

+197
-4
lines changed

5 files changed

+197
-4
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* `geom_sf()` now respects `arrow` parameter for lines (@jakeruss, #4659)
4+
35
* Updated documentation for `print.ggplot` to reflect that it returns
46
the original plot, not the result of `ggplot_build()`. (@r2evans, #4390)
57

R/geom-sf.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ GeomSf <- ggproto("GeomSf", Geom,
128128

129129
draw_panel = function(data, panel_params, coord, legend = NULL,
130130
lineend = "butt", linejoin = "round", linemitre = 10,
131-
na.rm = TRUE) {
131+
arrow = NULL, na.rm = TRUE) {
132132
if (!inherits(coord, "CoordSf")) {
133133
abort("geom_sf() must be used with coord_sf()")
134134
}
135135

136136
# Need to refactor this to generate one grob per geometry type
137137
coord <- coord$transform(data, panel_params)
138-
sf_grob(coord, lineend = lineend, linejoin = linejoin, linemitre = linemitre, na.rm = na.rm)
138+
sf_grob(coord, lineend = lineend, linejoin = linejoin, linemitre = linemitre,
139+
arrow = arrow, na.rm = na.rm)
139140
},
140141

141142
draw_key = function(data, params, size) {
@@ -160,7 +161,8 @@ default_aesthetics <- function(type) {
160161
}
161162
}
162163

163-
sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10, na.rm = TRUE) {
164+
sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10,
165+
arrow = NULL, na.rm = TRUE) {
164166
type <- sf_types[sf::st_geometry_type(x$geometry)]
165167
is_point <- type == "point"
166168
is_line <- type == "line"
@@ -210,7 +212,7 @@ sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10, na.
210212
col = col, fill = fill, fontsize = fontsize, lwd = lwd, lty = lty,
211213
lineend = lineend, linejoin = linejoin, linemitre = linemitre
212214
)
213-
sf::st_as_grob(x$geometry, pch = pch, gp = gp)
215+
sf::st_as_grob(x$geometry, pch = pch, gp = gp, arrow = arrow)
214216
}
215217

216218
#' @export
Lines changed: 72 additions & 0 deletions
Loading
Lines changed: 82 additions & 0 deletions
Loading

tests/testthat/test-geom-sf.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,38 @@ test_that("geom_sf_text() and geom_sf_label() draws correctly", {
200200
ggplot() + geom_sf_label(data = nc_3857, aes(label = NAME))
201201
)
202202
})
203+
204+
test_that("geom_sf draws arrows correctly", {
205+
skip_if_not_installed("sf")
206+
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
207+
208+
nc_tiny_coords <- data_frame(
209+
x = c(-81.473, -81.741, -81.67, -81.345, -81.266, -81.24, -81.473),
210+
y = c(36.234, 36.392, 36.59, 36.573, 36.437, 36.365, 36.234)
211+
)
212+
213+
nc <- sf::st_linestring(
214+
sf::st_coordinates(sf::st_as_sf(nc_tiny_coords, coords = c("x", "y"), crs = 4326))
215+
)
216+
217+
nc2 <- sf::st_cast(
218+
sf::st_sfc(
219+
sf::st_multilinestring(lapply(
220+
1:(length(sf::st_coordinates(nc)[, 1]) - 1),
221+
function(x) rbind(
222+
as.numeric(sf::st_coordinates(nc)[x, 1:2]),
223+
as.numeric(sf::st_coordinates(nc)[x + 1, 1:2])
224+
)
225+
)
226+
), sf::st_crs(nc)
227+
), "LINESTRING"
228+
)
229+
230+
expect_doppelganger("North Carolina county boundaries with arrow",
231+
ggplot() + geom_sf(data = nc, arrow = arrow()) + coord_sf(datum = 4326)
232+
)
233+
234+
expect_doppelganger("North Carolina county boundaries with more than one arrow",
235+
ggplot() + geom_sf(data = nc2, arrow = arrow()) + coord_sf(datum = 4326)
236+
)
237+
})

0 commit comments

Comments
 (0)