Cargamos el dataset que contiene datos de las escuelas de NY.
facilitiesNY <- st_read("/Users/nadia/Downloads/facilities_shp_201901/facilities_20190110.shp")
## Reading layer `facilities_20190110' from data source `/Users/nadia/Downloads/facilities_shp_201901/facilities_20190110.shp' using driver `ESRI Shapefile'
## Simple feature collection with 36925 features and 45 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: 913448.4 ymin: 121211.9 xmax: 1066854 ymax: 272251.8
## epsg (SRID): NA
## proj4string: +proj=lcc +lat_1=40.66666666666666 +lat_2=41.03333333333333 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs
Filtramos por escuelas:
escuelasNY <- filter(facilitiesNY, facgroup == "Schools (K-12)")
Ahora cargamos el dataset de los barrios de NY:
barriosNY <- st_read("/Users/nadia/Downloads/nynta_19b/nynta.shp")
## Reading layer `nynta' from data source `/Users/nadia/Downloads/nynta_19b/nynta.shp' using driver `ESRI Shapefile'
## Simple feature collection with 195 features and 7 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 913175.1 ymin: 120121.9 xmax: 1067383 ymax: 272844.3
## epsg (SRID): NA
## proj4string: +proj=lcc +lat_1=40.66666666666666 +lat_2=41.03333333333333 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs
Filtramos el dataset de las escuelas en NY:
escuelasNY <- escuelasNY %>%
filter(!is.na(latitude), !is.na(longitude)) %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326)
Teniendo ambos datasets de tipo espacial entonces graficamos en forma de puntos las escuelas que hay en NY:
ggplot() +
geom_sf(data = barriosNY) +
geom_sf(data = escuelasNY, alpha = .3, color = "blue")
Agrupamos y contabilizamos escuelas por barrios:
escuelas_por_barrio <- escuelasNY %>%
group_by(boro) %>%
summarise(cantidad = n())
Unificamos ambos datasets:
escuelas_barrios <- st_join(barriosNY, escuelas_por_barrio)
Hacemos un grafico donde vemos la cantidad de escuelas en los barrios:
ggplot() +
geom_sf(data = barriosNY) +
geom_sf(data = escuelas_barrios, aes(fill = cantidad)) +
labs(title = "Escuelas en barrios de NY",
subtitle = "Nueva York, Estados Unidos",
fill = "Cantidad de escuelas")
ggplot(escuelas_barrios) +
geom_bar(aes(x = boro, weight = cantidad)) +
labs(title = "Cantidad de escuelas por barrio en Nueva York",
x = "barrio",
y = "cantidad",
fill = "Cantidad de escuelas")
bbox <- getbb("brooklyn, New York city")
bbox_poly = getbb("brooklyn, New York city", format_out = "sf_polygon")
leaflet(bbox_poly) %>%
addTiles() %>%
addPolygons()
calles_bk <- opq(bbox) %>%
add_osm_feature(key = "highway") %>%
osmdata_sf()
bk_calles <- calles_bk$osm_lines
bk_calles <- st_intersection(bk_calles, bbox_poly)
ggplot() +
geom_sf(data = bk_calles)
ggplot() +
geom_sf(data = bk_calles,
color = "gray40", alpha = .5) +
geom_sf(data = filter(bk_calles, str_detect(name, "Avenue")),
color = "salmon") +
theme_void() +
labs(title = "Brooklyn",
subtitle = "Avenidas",
caption = "fuente: OpenStreetMap")
bk_library <- opq(bbox) %>%
add_osm_feature(key = "amenity", value = "library") %>%
osmdata_sf()
bk_library <- st_intersection(bk_library$osm_points, bbox_poly)
ggplot() +
geom_sf(data = bk_calles,
color = "darkslateblue") +
geom_sf_label_repel(data = bk_library,
aes(label = name), size = 2) +
theme_void() +
labs(title = "Brooklyn",
subtitle = "Bibliotecas",
caption = "fuente: OpenStreetMap")
Se ve en el mapa de Brooklyn que las bibliotecas se distribuyen mayormente sobre el sur de la ciudad.
bibliotecas_brooklyn <- bk_library %>%
group_by(addr.city) %>%
summarise(cantidad = n())
barriosNY <- st_transform(barriosNY,
st_crs(bibliotecas_brooklyn))
Unificamos ambos datasets:
bibliotecas_barrio <- st_join(barriosNY, bibliotecas_brooklyn)
ggplot() +
geom_sf(data = barriosNY) +
geom_sf(data = bibliotecas_barrio, aes(fill = cantidad)) +
labs(title = "Bibliotecas en Brooklyn",
subtitle = "Nueva York, Estados Unidos",
fill = "Cantidad de bibliotecas")