Skip to content

Stat for ploting sf data as label and text #2742

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

Closed
yutannihilation opened this issue Jul 8, 2018 · 10 comments · Fixed by #2761
Closed

Stat for ploting sf data as label and text #2742

yutannihilation opened this issue Jul 8, 2018 · 10 comments · Fixed by #2761

Comments

@yutannihilation
Copy link
Member

According to #2111, ggplot2 decided not to implement stat_sf(geom = "text") to avoid complexity.
But, I face the necessity to plot labels and texts on sf maps so often that I'm urged to make a package...

IIUC, it's not so complicated to implement a stat that retrieves the coordinates from a sf object and map the computed variables X and Y. Do you feel it's ok for this to live in ggplot2? If yes, I'm happy to make a PR. (If no, I will develop my package and submit it to CRAN.)

Here's an example implementation:

library(ggplot2)

StatSfCoordinates <- ggproto(
  "StatSfCoordinates", Stat,
  setup_params = function(data, params) {
    if (is.null(params$fun.geometry)) {
      params$fun.geometry <- sf::st_point_on_surface
    }
    params
  },
  compute_group = function(data, scales, fun.geometry) {
    points_sfc <- fun.geometry(data$geometry)
    coordinates <- sf::st_coordinates(points_sfc)
    data <- cbind(data, coordinates)
    
    data
  },
  
  default_aes = aes(x = stat(X), y = stat(Y)),
  required_aes = c("geometry")
)

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
ggplot(nc[1:3, ]) +
  geom_sf(aes(fill = AREA)) +
  geom_label(aes(geometry = geometry, label = NAME), stat = "sf_coordinates")
#> Warning in st_point_on_surface.sfc(data$geometry): st_point_on_surface may
#> not give correct results for longitude/latitude data

Created on 2018-07-08 by the reprex package (v0.2.0).

@thomasp85
Copy link
Member

I’ve been thinking about this as well, and can’t see how much of extra complexity it would add. If it’s decided not to be put in ggplot2 I would welcome it in ggforce

@hadley
Copy link
Member

hadley commented Jul 8, 2018

If you think you can implement it without adding a bunch of extra options (and default aesthetics) to geom_sf(), I'd be happy to consider a PR. But maybe geom_sf_label() would be better?

@yutannihilation
Copy link
Member Author

Thanks! Actually, geom_sf_label() (and geom_sf_text()) are what I've had in my mind :)

c.f. https://yutannihilation.github.io/ggsflabel/reference/geom_sf_label.html

I expect implementing them doesn't affect geom_sf().

@hadley
Copy link
Member

hadley commented Jul 8, 2018

Yeah that sounds good and I’d be happy to review a PR. I’d probably suggest documenting with geom_sf

@yutannihilation
Copy link
Member Author

Sure!

@thomasp85
Are you interested in including geom_sf_label_repel() and geom_sf_text_repel() in ggforce? If so, I'll abandon my package.

@thomasp85
Copy link
Member

Yes

@clauswilke
Copy link
Member

@yutannihilation Maybe you can talk to the maintainer of ggrepel whether they can be added there directly? Seems like the more logical place.

@yutannihilation
Copy link
Member Author

@clauswilke Agreed, thanks!

@adrfantini
Copy link

adrfantini commented Aug 14, 2018

This is also a feature I use from time to time (doing things manually, with st_centroid), thanks for taking the time to add it!

clauswilke pushed a commit that referenced this issue Aug 24, 2018
Closes #2742

* Add geom_sf_label() and geom_sf_text()

* Add a missing parenthesis

* Add tests for stat_sf_coordinates()

* Fix a typo

* Fix a typo in examples

* Add visual tests for geom_sf_label() and geom_sf_text()

* Match args for stat_sf_coordinates() and StatSfCoordinates$compute_group()

* Document stat_sf_coordinates()'s na.rm

* Add documents about stat_sf_coordinats()

* Set the default of fun.geometry to NULL

When sf package is not installed, test-function-args fails.

* Set more fun.geometry to NULL

* Fix mistakenly passed sf::point_on_surface

* Stop cross-referencing sf functions

* Ignore Z and M dimension

* Fix the example of stat_sf_coordinates()

* Remove Rplot001.png

* Fix doc of stat_sf_coordinates()

* Fix an example of stat_sf_coordinates()

* Fix default fun.geometry and tests

* Fix a typo in doc

* Add reference images for vdiffr tests

* Disable stat-sf-coordinates test

* Fix "texts" to "text", and move seealso

* Add () to functions in ggsf.Rd for consitency

* Add a news bullet
@lock
Copy link

lock bot commented Feb 20, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Feb 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants