Created
May 8, 2017 22:57
-
-
Save jcheng5/63adf1ebb76b32dc014f2004ab8ba6bb to your computer and use it in GitHub Desktop.
Convert ggfortify to sf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
convertToSF <- function(df, id = "id", group = "group") { | |
# Extract the non-polygon data. | |
data <- df %>% | |
group_by_(id) %>% | |
do(head(., 1)) %>% | |
.[,setdiff(names(.), c("long", "lat", "order", "hole", "piece", "group"))] | |
# Extract the shapes. I don't know if this does the right | |
# thing with holes--are holes even supported by ggfortify? | |
features <- state.map %>% | |
group_by_(id, group) %>% | |
do(shape = list(cbind(.$long, .$lat))) %>% | |
ungroup() %>% | |
group_by_(id) %>% | |
do(shapes = sf::st_multipolygon(.$shape)) %>% | |
.$shapes %>% | |
sf::st_sfc() | |
sf::st_sf(features, data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment