Skip to content

Add arrow to line params#4659

Merged
thomasp85 merged 9 commits into
tidyverse:mainfrom
JakeRuss:main
Nov 9, 2021
Merged

Add arrow to line params#4659
thomasp85 merged 9 commits into
tidyverse:mainfrom
JakeRuss:main

Conversation

@JakeRuss

@JakeRuss JakeRuss commented Nov 4, 2021

Copy link
Copy Markdown
Contributor

My vague recollection when I opened #4391, was I thought adding arrow was basically adding it to the list. As you can see from this commit it doesn't work.

I expected the following to work, but no.

library(sf)
library(ggplot2)

coords <- tibble::tibble(lat = c(26.562855, 28.538336), 
                         lon = c(-81.949532, -81.379234)) %>%
  st_as_sf(coords = c("lon", "lat"),  crs = 4326, remove = FALSE) %>%
  st_coordinates() %>%
  st_linestring()

ggplot() + 
  geom_sf(data = coords, size = 2, 
          arrow = arrow(angle = 45, ends = "last", type = "open", length = unit(0.5, "inches")))

I'm not sure I understand enough to complete this task

@clauswilke

Copy link
Copy Markdown
Member

Arrows are not set in gpar(), they are set directly in the grob. See e.g. the documentation to linesGrob(). I suspect that if you add the arrow info here it will work:

sf::st_as_grob(x$geometry, pch = pch, gp = gp)

@JakeRuss

JakeRuss commented Nov 4, 2021

Copy link
Copy Markdown
Contributor Author

Thank you @clauswilke, that does seem to have done the trick. My example above now works.

I guess add a bullet to news. Is there anything else I need to add here?

@clauswilke

Copy link
Copy Markdown
Member

Maybe also add a visual test?

@JakeRuss

JakeRuss commented Nov 4, 2021

Copy link
Copy Markdown
Contributor Author

My apologies, but I'm not sure what that means. If you could point me to an example I'd gladly add it.

@clauswilke

Copy link
Copy Markdown
Member

See: https://vdiffr.r-lib.org/

Example:

test_that("geom_sf_text() and geom_sf_label() draws correctly", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
nc_tiny_coords <- matrix(
c(-81.473, -81.741, -81.67, -81.345, -81.266, -81.24, -81.473,
36.234, 36.392, 36.59, 36.573, 36.437, 36.365, 36.234),
ncol = 2
)
nc <- sf::st_as_sf(
data_frame(
NAME = "ashe",
geometry = sf::st_sfc(sf::st_polygon(list(nc_tiny_coords)), crs = 4326)
)
)
# In order to avoid warning, transform to a projected coordinate system
nc_3857 <- sf::st_transform(nc, 3857)
expect_doppelganger("Texts for North Carolina",
ggplot() + geom_sf_text(data = nc_3857, aes(label = NAME))
)
expect_doppelganger("Labels for North Carolina",
ggplot() + geom_sf_label(data = nc_3857, aes(label = NAME))
)
})

@JakeRuss

JakeRuss commented Nov 5, 2021

Copy link
Copy Markdown
Contributor Author

Greatly appreciated, I will add

@JakeRuss

JakeRuss commented Nov 5, 2021

Copy link
Copy Markdown
Contributor Author

@thomasp85 Ready to review at your schedule

@clauswilke

Copy link
Copy Markdown
Member

@JakeRuss It looks like you didn't commit the new reference images for the two visual tests.

@JakeRuss

JakeRuss commented Nov 5, 2021

Copy link
Copy Markdown
Contributor Author

@clauswilke Thank you for all of your pointers

@clauswilke

Copy link
Copy Markdown
Member

The tests are failing. Please take a look. Looks like you need to write sf::st_coordinates() instead of just st_coordinates(). There may be other, similar issues once you fix this one.

@clauswilke

Copy link
Copy Markdown
Member

data_frame() is a function defined by the ggplot2 codebase. It should not require a tibble:: prefix. See also the other tests.

@clauswilke

Copy link
Copy Markdown
Member

The reason why some of the tests still fail is because the reference images differ between the version you committed and the version generated in the github action. You may have outdated versions of some libraries. Not clear which ones, but if not vdiffr then possibly sf.

@JakeRuss

JakeRuss commented Nov 8, 2021

Copy link
Copy Markdown
Contributor Author

Thanks, I will clean this up again today. I was running the testthat tests locally and it was complaining about data_frame, which I see now was because I didn't have it loaded.

@clauswilke clauswilke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@thomasp85 Any comments or concerns from your end?

@thomasp85
thomasp85 merged commit cad7f78 into tidyverse:main Nov 9, 2021
@thomasp85

Copy link
Copy Markdown
Member

Thanks @JakeRuss !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants