paleolimbot / ggspatial

Enhancing spatial visualization in ggplot2
https://paleolimbot.github.io/ggspatial
368 stars 34 forks source link

df_spatial and vertex identification #76

Closed asherfie closed 3 years ago

asherfie commented 3 years ago

Hello, in a previous version of ggspatial (1.0.3) when creating a dataframe out of a polygon shapefile using df_spatial there would be a column labeled coordinate_id that allowed for each vertex of the polygon to be uniquely identifiable. In the newest version of ggspatial (1.1.4) this column no longer exists. Is there a new way to identify an individual vertex or add the coordinate_id column back into the dataframe?

paleolimbot commented 3 years ago

I forget the details of why that column is no longer there (my guess is that it took too long to calculate it and it wasn't used all that often). You can calculate yourself using dplyr (and possibly other ways):

library(ggspatial)
nc <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))
(df <- df_spatial(nc))
#> # A tibble: 2,529 x 19
#>        x     y feature_id part_id piece_id  AREA PERIMETER CNTY_ CNTY_ID NAME 
#>    <dbl> <dbl>      <int>   <int>    <int> <dbl>     <dbl> <dbl>   <dbl> <chr>
#>  1 -81.5  36.2          1       1        1 0.114      1.44  1825    1825 Ashe 
#>  2 -81.5  36.3          1       1        1 0.114      1.44  1825    1825 Ashe 
#>  3 -81.6  36.3          1       1        1 0.114      1.44  1825    1825 Ashe 
#>  4 -81.6  36.3          1       1        1 0.114      1.44  1825    1825 Ashe 
#>  5 -81.7  36.4          1       1        1 0.114      1.44  1825    1825 Ashe 
#>  6 -81.7  36.5          1       1        1 0.114      1.44  1825    1825 Ashe 
#>  7 -81.7  36.5          1       1        1 0.114      1.44  1825    1825 Ashe 
#>  8 -81.7  36.6          1       1        1 0.114      1.44  1825    1825 Ashe 
#>  9 -81.3  36.6          1       1        1 0.114      1.44  1825    1825 Ashe 
#> 10 -81.3  36.5          1       1        1 0.114      1.44  1825    1825 Ashe 
#> # … with 2,519 more rows, and 9 more variables: FIPS <chr>, FIPSNO <dbl>,
#> #   CRESS_ID <int>, BIR74 <dbl>, SID74 <dbl>, NWBIR74 <dbl>, BIR79 <dbl>,
#> #   SID79 <dbl>, NWBIR79 <dbl>

library(dplyr)
df %>% 
  group_by(piece_id) %>% 
  mutate(coord_id = 1:n()) %>% 
  ungroup() %>% 
  select(x, y, feature_id, part_id, piece_id, coord_id)
#> # A tibble: 2,529 x 6
#>        x     y feature_id part_id piece_id coord_id
#>    <dbl> <dbl>      <int>   <int>    <int>    <int>
#>  1 -81.5  36.2          1       1        1        1
#>  2 -81.5  36.3          1       1        1        2
#>  3 -81.6  36.3          1       1        1        3
#>  4 -81.6  36.3          1       1        1        4
#>  5 -81.7  36.4          1       1        1        5
#>  6 -81.7  36.5          1       1        1        6
#>  7 -81.7  36.5          1       1        1        7
#>  8 -81.7  36.6          1       1        1        8
#>  9 -81.3  36.6          1       1        1        9
#> 10 -81.3  36.5          1       1        1       10
#> # … with 2,519 more rows

Created on 2021-01-03 by the reprex package (v0.3.0)