Skip to content

Add fortify() method for sfc and sfg #2685

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 1 commit into from
Jun 5, 2018

Conversation

yutannihilation
Copy link
Member

It happens often that an sf object is converted to sfc as the result of some operation (e.g. UNION).

library(ggplot2)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3

nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

nc_outer_lines <- nc %>%
  st_union() %>%
  st_boundary()

nc_inner_lines <- nc %>%
  st_boundary() %>%
  st_union() %>%
  st_line_merge()

ggplot() +
  geom_sf(data = nc, colour = "transparent", fill = "lightgreen") +
  geom_sf(data = nc_inner_lines, colour = "black",  linetype = "11", size = 1) +
  geom_sf(data = nc_outer_lines, colour = "black", size = 1.5) +
  theme_minimal()
#> Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class sfc_MULTILINESTRING/sfc

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

It's not a big deal to convert them back to st_sf() manually like below, but it's nice and handy if they are converted automatically. As plot() can plot sfc and sfg objects, I expect ggplot2 can plot these as well.

ggplot() +
  geom_sf(data = nc, colour = "transparent", fill = "lightgreen") +
  geom_sf(data = st_sf(nc_inner_lines), colour = "black",  linetype = "11", size = 1) +
  geom_sf(data = st_sf(nc_outer_lines), colour = "black", size = 1.5) +
  theme_minimal()

(The expected result:)

image

@hadley hadley merged commit 4db5122 into tidyverse:master Jun 5, 2018
@hadley
Copy link
Member

hadley commented Jun 5, 2018

Thanks!!

@yutannihilation yutannihilation deleted the fortify-sfc branch June 5, 2018 23:36
@lock
Copy link

lock bot commented Dec 3, 2018

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 Dec 3, 2018
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 this pull request may close these issues.

2 participants