Skip to content

Instantly share code, notes, and snippets.

@juanchiem
Last active March 25, 2021 13:40
Show Gist options
  • Save juanchiem/2ebba583662309247511612f5f0e0f99 to your computer and use it in GitHub Desktop.
Save juanchiem/2ebba583662309247511612f5f0e0f99 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(sf)
theme_set(theme_bw()+
theme(
panel.grid.major = element_line(color = gray(0.5),
linetype = "dashed",
size = 0.1),
panel.background = element_rect(fill = "aliceblue"),
axis.text.x = element_text(size = 6),
axis.text.y = element_text(size = 6),
))
ARG <- raster::getData(name = "GADM", country = "ARG", level = 0) %>%
st_as_sf()
BSAS <- raster::getData(name = "GADM", country = "ARG", level = 2) %>%
st_as_sf() %>%
filter(NAME_1 == "Buenos Aires")
BSAS %>%
mutate(Zona = case_when(
str_detect(NAME_2, "Madariaga|Maipú|Chiquita|Ayacucho") ~ "Madariaga",
str_detect(NAME_2, "Balcarce|Lober") ~ "Sierra",
str_detect(NAME_2, "Pueyrre|Alvarado") ~ "Costa")) %>%
drop_na(Zona) %>%
mutate(id = gsub("[^A-Z]+", "", .$NAME_2)) -> jmf
jmf <- cbind(jmf, st_coordinates(st_centroid(jmf$geometry))) %>%
mutate(has = st_area(geometry)/10000)
jmf %>%
group_by(Zona) %>%
summarise(has = sum(has))
ARG %>%
ggplot()+
geom_sf()+
geom_sf(data = BSAS, fill = "gray98", color= "gray60", size =0.2) +
geom_sf(data = jmf,
aes(fill = Zona), color = gray(.5), size = 0.2) +
coord_sf(xlim = c(-60,-56.5), ylim = c(-39, -36), expand = FALSE,
label_axes = waiver())+
geom_text(data = jmf, aes(x = X, y = Y, label = id),
size = 3, hjust = 0.5, fontface = "bold")+
labs(x = "", y = "")
@juanchiem
Copy link
Author

image

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