From 6d19950ec69e18dce119ae121059bae554ce421f Mon Sep 17 00:00:00 2001 From: Jake Russ Date: Thu, 4 Nov 2021 15:19:22 -0400 Subject: [PATCH 1/9] Add arrow to line params --- R/geom-sf.R | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/R/geom-sf.R b/R/geom-sf.R index 4367ade3be..b77cfa4dbb 100644 --- a/R/geom-sf.R +++ b/R/geom-sf.R @@ -128,14 +128,15 @@ GeomSf <- ggproto("GeomSf", Geom, draw_panel = function(data, panel_params, coord, legend = NULL, lineend = "butt", linejoin = "round", linemitre = 10, - na.rm = TRUE) { + arrow = NULL, na.rm = TRUE) { if (!inherits(coord, "CoordSf")) { abort("geom_sf() must be used with coord_sf()") } # Need to refactor this to generate one grob per geometry type coord <- coord$transform(data, panel_params) - sf_grob(coord, lineend = lineend, linejoin = linejoin, linemitre = linemitre, na.rm = na.rm) + sf_grob(coord, lineend = lineend, linejoin = linejoin, linemitre = linemitre, + arrow = arrow, na.rm = na.rm) }, draw_key = function(data, params, size) { @@ -160,7 +161,8 @@ default_aesthetics <- function(type) { } } -sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10, na.rm = TRUE) { +sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10, + arrow = NULL, na.rm = TRUE) { type <- sf_types[sf::st_geometry_type(x$geometry)] is_point <- type == "point" is_line <- type == "line" @@ -208,7 +210,8 @@ sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10, na. lty <- x$linetype %||% defaults$linetype[type_ind] gp <- gpar( col = col, fill = fill, fontsize = fontsize, lwd = lwd, lty = lty, - lineend = lineend, linejoin = linejoin, linemitre = linemitre + lineend = lineend, linejoin = linejoin, linemitre = linemitre, + arrow = arrow ) sf::st_as_grob(x$geometry, pch = pch, gp = gp) } From 727a82c00f4c629f8328ddc6c9081ce410e0ba68 Mon Sep 17 00:00:00 2001 From: Jake Russ Date: Thu, 4 Nov 2021 16:03:44 -0400 Subject: [PATCH 2/9] Move arrow to st_as_grob --- R/geom-sf.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/geom-sf.R b/R/geom-sf.R index b77cfa4dbb..0111c6fd5b 100644 --- a/R/geom-sf.R +++ b/R/geom-sf.R @@ -210,10 +210,9 @@ sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10, lty <- x$linetype %||% defaults$linetype[type_ind] gp <- gpar( col = col, fill = fill, fontsize = fontsize, lwd = lwd, lty = lty, - lineend = lineend, linejoin = linejoin, linemitre = linemitre, - arrow = arrow + lineend = lineend, linejoin = linejoin, linemitre = linemitre ) - sf::st_as_grob(x$geometry, pch = pch, gp = gp) + sf::st_as_grob(x$geometry, pch = pch, gp = gp, arrow = arrow) } #' @export From b1bc8fc9601f315b5565ead9722ee79024359946 Mon Sep 17 00:00:00 2001 From: Jake Russ Date: Fri, 5 Nov 2021 14:39:25 -0400 Subject: [PATCH 3/9] Add visual tests for single and multiple arrows --- tests/testthat/test-geom-sf.R | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/testthat/test-geom-sf.R b/tests/testthat/test-geom-sf.R index f09e85ed3e..651a128177 100644 --- a/tests/testthat/test-geom-sf.R +++ b/tests/testthat/test-geom-sf.R @@ -202,3 +202,38 @@ test_that("geom_sf_text() and geom_sf_label() draws correctly", { ggplot() + geom_sf_label(data = nc_3857, aes(label = NAME)) ) }) + +test_that("geom_sf draws arrows correctly", { + skip_if_not_installed("sf") + if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3") + + nc_tiny_coords <- data_frame( + x = c(-81.473, -81.741, -81.67, -81.345, -81.266, -81.24, -81.473), + y = c(36.234, 36.392, 36.59, 36.573, 36.437, 36.365, 36.234) + ) + + nc <- sf::st_linestring( + sf::st_coordinates(sf::st_as_sf(nc_tiny_coords, coords = c("x", "y"), crs = 4326)) + ) + + nc2 <- sf::st_cast( + sf::st_sfc( + sf::st_multilinestring(lapply( + 1:(length(st_coordinates(nc)[, 1]) - 1), + function(x) rbind( + as.numeric(st_coordinates(nc)[x, 1:2]), + as.numeric(st_coordinates(nc)[x + 1, 1:2]) + ) + ) + ), sf::st_crs(nc) + ), "LINESTRING" + ) + + expect_doppelganger("North Carolina county boundaries with arrow", + ggplot() + geom_sf(data = nc, arrow = arrow()) + coord_sf(datum = 4326) + ) + + expect_doppelganger("North Carolina county boundaries with more than one arrow", + ggplot() + geom_sf(data = nc2, arrow = arrow()) + coord_sf(datum = 4326) + ) +}) From c6ee96de599be454ad61a34919b6fa9504177430 Mon Sep 17 00:00:00 2001 From: Jake Russ Date: Fri, 5 Nov 2021 14:42:37 -0400 Subject: [PATCH 4/9] Add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 939d829a49..d2ed125098 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* `geom_sf()` now respects `arrow` parameter for lines (@jakeruss, #4659) + * Updated documentation for `print.ggplot` to reflect that it returns the original plot, not the result of `ggplot_build()`. (@r2evans, #4390) From 6e847a0f58c69756b343bc07b0bd29b41bc206e3 Mon Sep 17 00:00:00 2001 From: Jake Russ Date: Fri, 5 Nov 2021 15:02:47 -0400 Subject: [PATCH 5/9] Add SVG files for the tests --- ...-carolina-county-boundaries-with-arrow.svg | 71 ++++++++++++++++ ...ty-boundaries-with-more-than-one-arrow.svg | 81 +++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-arrow.svg create mode 100644 tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-more-than-one-arrow.svg diff --git a/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-arrow.svg b/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-arrow.svg new file mode 100644 index 0000000000..d01bd6ffb3 --- /dev/null +++ b/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-arrow.svg @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +36.25 +36.30 +36.35 +36.40 +36.45 +36.50 +36.55 +36.60 + + + + + + + + + + + + + +-81.7 +-81.6 +-81.5 +-81.4 +-81.3 + + diff --git a/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-more-than-one-arrow.svg b/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-more-than-one-arrow.svg new file mode 100644 index 0000000000..bd725857cc --- /dev/null +++ b/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-more-than-one-arrow.svg @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +36.25 +36.30 +36.35 +36.40 +36.45 +36.50 +36.55 +36.60 + + + + + + + + + + + + + +-81.7 +-81.6 +-81.5 +-81.4 +-81.3 + + From 41b9709efb1544c5a104b9786e8a7df931556272 Mon Sep 17 00:00:00 2001 From: Jake Russ Date: Fri, 5 Nov 2021 15:37:20 -0400 Subject: [PATCH 6/9] Add package references --- tests/testthat/test-geom-sf.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-geom-sf.R b/tests/testthat/test-geom-sf.R index 651a128177..4245de9c90 100644 --- a/tests/testthat/test-geom-sf.R +++ b/tests/testthat/test-geom-sf.R @@ -219,10 +219,10 @@ test_that("geom_sf draws arrows correctly", { nc2 <- sf::st_cast( sf::st_sfc( sf::st_multilinestring(lapply( - 1:(length(st_coordinates(nc)[, 1]) - 1), + 1:(length(sf::st_coordinates(nc)[, 1]) - 1), function(x) rbind( - as.numeric(st_coordinates(nc)[x, 1:2]), - as.numeric(st_coordinates(nc)[x + 1, 1:2]) + as.numeric(sf::st_coordinates(nc)[x, 1:2]), + as.numeric(sf::st_coordinates(nc)[x + 1, 1:2]) ) ) ), sf::st_crs(nc) From 085ca42265070a509f355e70542f240df85451bb Mon Sep 17 00:00:00 2001 From: Jake Russ Date: Fri, 5 Nov 2021 15:50:39 -0400 Subject: [PATCH 7/9] Add another package ref --- tests/testthat/test-geom-sf.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-geom-sf.R b/tests/testthat/test-geom-sf.R index 4245de9c90..48e4b4eb9f 100644 --- a/tests/testthat/test-geom-sf.R +++ b/tests/testthat/test-geom-sf.R @@ -207,7 +207,7 @@ test_that("geom_sf draws arrows correctly", { skip_if_not_installed("sf") if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3") - nc_tiny_coords <- data_frame( + nc_tiny_coords <- tibble::data_frame( x = c(-81.473, -81.741, -81.67, -81.345, -81.266, -81.24, -81.473), y = c(36.234, 36.392, 36.59, 36.573, 36.437, 36.365, 36.234) ) From 7a5347e0be4280c410485cb593a10d8cca64bf89 Mon Sep 17 00:00:00 2001 From: Jake Russ Date: Mon, 8 Nov 2021 11:32:41 -0500 Subject: [PATCH 8/9] Remove tibble reference --- tests/testthat/test-geom-sf.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-geom-sf.R b/tests/testthat/test-geom-sf.R index 48e4b4eb9f..a0824c2d68 100644 --- a/tests/testthat/test-geom-sf.R +++ b/tests/testthat/test-geom-sf.R @@ -207,7 +207,7 @@ test_that("geom_sf draws arrows correctly", { skip_if_not_installed("sf") if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3") - nc_tiny_coords <- tibble::data_frame( + nc_tiny_coords <- data_frame( x = c(-81.473, -81.741, -81.67, -81.345, -81.266, -81.24, -81.473), y = c(36.234, 36.392, 36.59, 36.573, 36.437, 36.365, 36.234) ) @@ -230,10 +230,10 @@ test_that("geom_sf draws arrows correctly", { ) expect_doppelganger("North Carolina county boundaries with arrow", - ggplot() + geom_sf(data = nc, arrow = arrow()) + coord_sf(datum = 4326) + ggplot() + geom_sf(data = nc, arrow = arrow()) + coord_sf(datum = 4326) ) expect_doppelganger("North Carolina county boundaries with more than one arrow", - ggplot() + geom_sf(data = nc2, arrow = arrow()) + coord_sf(datum = 4326) + ggplot() + geom_sf(data = nc2, arrow = arrow()) + coord_sf(datum = 4326) ) }) From 8b8bea114fb8f00f0ffff0c971c9711fca725cf9 Mon Sep 17 00:00:00 2001 From: Jake Russ Date: Mon, 8 Nov 2021 11:32:52 -0500 Subject: [PATCH 9/9] Update SVG files --- ...-carolina-county-boundaries-with-arrow.svg | 75 +++++++-------- ...ty-boundaries-with-more-than-one-arrow.svg | 95 ++++++++++--------- 2 files changed, 86 insertions(+), 84 deletions(-) diff --git a/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-arrow.svg b/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-arrow.svg index d01bd6ffb3..48beafdcdd 100644 --- a/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-arrow.svg +++ b/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-arrow.svg @@ -20,52 +20,53 @@ - - + + - - + + - - + + - - - - - + + + + + -36.25 -36.30 -36.35 -36.40 -36.45 -36.50 -36.55 -36.60 - - - - - - - - - - - - - --81.7 --81.6 --81.5 --81.4 --81.3 +36.25 +36.30 +36.35 +36.40 +36.45 +36.50 +36.55 +36.60 + + + + + + + + + + + + + +-81.7 +-81.6 +-81.5 +-81.4 +-81.3 +North Carolina county boundaries with arrow diff --git a/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-more-than-one-arrow.svg b/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-more-than-one-arrow.svg index bd725857cc..165452b12f 100644 --- a/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-more-than-one-arrow.svg +++ b/tests/testthat/_snaps/geom-sf/north-carolina-county-boundaries-with-more-than-one-arrow.svg @@ -20,62 +20,63 @@ - - + + - - + + - - + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + -36.25 -36.30 -36.35 -36.40 -36.45 -36.50 -36.55 -36.60 - - - - - - - - - - - - - --81.7 --81.6 --81.5 --81.4 --81.3 +36.25 +36.30 +36.35 +36.40 +36.45 +36.50 +36.55 +36.60 + + + + + + + + + + + + + +-81.7 +-81.6 +-81.5 +-81.4 +-81.3 +North Carolina county boundaries with more than one arrow