Comenzamos cargando las librerías a utilizar
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.1
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
## -- Attaching packages ----------------------------------------------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.1 v purrr 0.3.2
## v tibble 2.1.1 v dplyr 0.8.1
## v tidyr 0.8.3 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## -- Conflicts -------------------------------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
library(sf)
## Warning: package 'sf' was built under R version 3.6.1
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(rgdal)
## Warning: package 'rgdal' was built under R version 3.6.1
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.6.1
## rgdal: version: 1.4-4, (SVN revision 833)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
## Path to GDAL shared files: C:/Users/fsciutto/Documents/R/R-3.6.0/library/rgdal/gdal
## GDAL binary built with GEOS: TRUE
## Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
## Path to PROJ.4 shared files: C:/Users/fsciutto/Documents/R/R-3.6.0/library/rgdal/proj
## Linking to sp version: 1.3-1
require(PBSmapping)
## Loading required package: PBSmapping
## Warning: package 'PBSmapping' was built under R version 3.6.1
##
## -----------------------------------------------------------
## PBS Mapping 2.72.1 -- Copyright (C) 2003-2019 Fisheries and Oceans Canada
##
## PBS Mapping comes with ABSOLUTELY NO WARRANTY;
## for details see the file COPYING.
## This is free software, and you are welcome to redistribute
## it under certain conditions, as outlined in the above file.
##
## A complete user guide 'PBSmapping-UG.pdf' is located at
## C:/Users/fsciutto/Documents/R/R-3.6.0/library/PBSmapping/doc/PBSmapping-UG.pdf
##
## Packaged on 2019-03-14
## Pacific Biological Station, Nanaimo
##
## All available PBS packages can be found at
## https://github.com/pbs-software
##
## To see demos, type '.PBSfigs()'.
## -----------------------------------------------------------
require(maptools)
## Loading required package: maptools
## Warning: package 'maptools' was built under R version 3.6.1
## Checking rgeos availability: FALSE
## Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
## which has a restricted licence. It is disabled by default;
## to enable gpclib, type gpclibPermit()
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 3.6.1
Cargamos los shapes a unir y exploramos
barrios <- st_read("Boston_Neighborhoods.geojson")
## Reading layer `Boston_Neighborhoods' from data source `D:\ELISA\[B]ACADEMICA\FORMACION\[02]MEU\CIENCIA DE DATOS II - A BRUST\C1\TAREA\Boston_Neighborhoods.geojson' using driver `GeoJSON'
## Simple feature collection with 26 features and 7 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -71.19125 ymin: 42.22792 xmax: -70.92278 ymax: 42.39699
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
escuelas <- st_read ("Public_Schools.geojson")
## Reading layer `Public_Schools' from data source `D:\ELISA\[B]ACADEMICA\FORMACION\[02]MEU\CIENCIA DE DATOS II - A BRUST\C1\TAREA\Public_Schools.geojson' using driver `GeoJSON'
## Simple feature collection with 131 features and 16 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -71.17434 ymin: 42.2339 xmax: -71.00412 ymax: 42.39162
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
barrios
## Simple feature collection with 26 features and 7 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -71.19125 ymin: 42.22792 xmax: -70.92278 ymax: 42.39699
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
## First 10 features:
## OBJECTID Name Acres Neighborhood_ID SqMiles
## 1 27 Roslindale 1605.56824 15 2.51
## 2 28 Jamaica Plain 2519.24539 11 3.94
## 3 29 Mission Hill 350.85356 13 0.55
## 4 30 Longwood 188.61195 28 0.29
## 5 31 Bay Village 26.53984 33 0.04
## 6 32 Leather District 15.63991 27 0.02
## 7 33 Chinatown 76.32441 26 0.12
## 8 34 North End 126.91044 14 0.20
## 9 35 Roxbury 2108.46907 16 3.29
## 10 36 South End 471.53536 32 0.74
## ShapeSTArea ShapeSTLength geometry
## 1 69938272.9 53563.913 MULTIPOLYGON (((-71.12593 4...
## 2 109737890.8 56349.937 MULTIPOLYGON (((-71.10499 4...
## 3 15283120.0 17918.724 MULTIPOLYGON (((-71.09043 4...
## 4 8215903.5 11908.757 MULTIPOLYGON (((-71.09811 4...
## 5 1156070.8 4650.635 MULTIPOLYGON (((-71.06663 4...
## 6 681271.7 3237.141 MULTIPOLYGON (((-71.05838 4...
## 7 3324678.0 9736.590 MULTIPOLYGON (((-71.05791 4...
## 8 5527506.0 16177.827 MULTIPOLYGON (((-71.052 42....
## 9 91844546.0 49488.800 MULTIPOLYGON (((-71.09646 4...
## 10 20539997.9 17912.334 MULTIPOLYGON (((-71.06834 4...
escuelas
## Simple feature collection with 131 features and 16 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -71.17434 ymin: 42.2339 xmax: -71.00412 ymax: 42.39162
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
## First 10 features:
## OBJECTID_1 OBJECTID BLDG_ID BLDG_NAME ADDRESS
## 1 1 1 1 Guild Bldg 195 Leyden Street
## 2 2 2 3 Kennedy, P Bldg 343 Saratoga Street
## 3 3 3 4 Otis Bldg 218 Marion Street
## 4 4 4 6 Odonnell Bldg 33 Trenton Street
## 5 5 5 7 East Boston High Bldg 86 White Street
## 6 6 6 8 Umana / Barnes Bldg 312 Border Street
## 7 7 7 10 East Boston Eec Bldg 135 Gove Street
## 8 8 8 11 Mckay Bldg 122 Cottage Street
## 9 9 9 12 Adams Bldg 165 Webster Street
## 10 10 10 13 Harvard-Kent 50 Bunker Hill Street
## CITY ZIPCODE CSP_SCH_ID SCH_ID SCH_NAME
## 1 East Boston 02128 4061 4061 Guild Elementary
## 2 East Boston 02128 4541 4541 Kennedy Patrick Elem
## 3 East Boston 02128 4322 4322 Otis Elementary
## 4 East Boston 02128 4543 4543 O'Donnell Elementary
## 5 East Boston 02128 1070 1070 East Boston High
## 6 East Boston 02128 4323 4323 Umana Academy
## 7 East Boston 02128 4450 4450 East Boston EEC
## 8 East Boston 02128 4360 4360 McKay K-8
## 9 East Boston 02128 4361 4361 Adams Elementary
## 10 Charlestown 02129 4280 4280 Harvard/Kent Elem
## SCH_LABEL SCH_TYPE SHARED COMPLEX POINT_X POINT_Y
## 1 Guild ES 790128.2 2967094
## 2 PJ Kennedy ES 783027.7 2963318
## 3 Otis ES 782112.8 2962122
## 4 O'Donnell ES 780994.0 2963140
## 5 East Boston HS HS 781823.0 2964190
## 6 Umana Academy K-8 780367.0 2963210
## 7 East Boston EEC ELC 782062.0 2960080
## 8 McKay K-8 K-8 782012.1 2959933
## 9 Adams ES 781862.0 2958580
## 10 Harvard/Kent ES 775733.0 2962580
## geometry
## 1 POINT (-71.00412 42.3888)
## 2 POINT (-71.03048 42.37855)
## 3 POINT (-71.03389 42.37528)
## 4 POINT (-71.03801 42.37809)
## 5 POINT (-71.03492 42.38096)
## 6 POINT (-71.04033 42.37829)
## 7 POINT (-71.03412 42.36968)
## 8 POINT (-71.03431 42.36927)
## 9 POINT (-71.03489 42.36556)
## 10 POINT (-71.05749 42.37663)
Previsualizamos la data
ggplot()+
geom_sf(data= barrios)+
geom_sf(data=escuelas)
A los efectos de futura visualizacion vamos a hallar los centroides de los poligonos de barrios.
barriosCent<- st_centroid(barrios)
## Warning in st_centroid.sf(barrios): st_centroid assumes attributes are
## constant over geometries of x
## Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
## of_largest_polygon): st_centroid does not give correct centroids for
## longitude/latitude data
ggplot()+
geom_sf(data=barriosCent)
Ahora vamos a hacer el join espacial entre la data de barrios y escuelas. Primero, asignamos la informacion de barrios a los puntos de las escuelas.
escuelas <- st_join(escuelas, barrios)
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
Luego agrupamos las escuelas por barrio y contabilizamos. Nota: anulamos en este paso la informacion geometrica del shape, que traiamos residualmente para que no interfiera en el paso siguiente.
escuelasXbarrio <- escuelas %>%
group_by(Name)%>%
summarise(total=n()) %>%
st_set_geometry(NULL)
Acto seguido, hacemos un left join para asignarle al shape de barrios la data de escuelas que le corresponde a cada poligono. Visualizamos a partir de un “Choropleth map” de escuelas por barrio. Nota: Para mapear las etiquetas reordenamos las cordenadas en x, y.
barriosCent_xy <- as.data.frame(sf::st_coordinates(barriosCent))
barriosCent_xy$Name <- barrios$Name
barrios %>%
left_join(escuelasXbarrio)%>%
ggplot() +
geom_sf(aes(fill = total), color = NA)+
geom_sf(data=barriosCent, size=.5)+
geom_text_repel(data = barriosCent_xy, aes(X, Y, label = Name), colour = "black", size=3)
## Joining, by = "Name"
Por ultimo Visualizamos en un grafico de barras la cantidad de escuelas por barrios
ggplot(escuelasXbarrio) +
geom_bar(aes(x = Name, weight = total, fill = total)) +
coord_flip()+
labs(title= "Boston",
subtitle="Escuelas por barrio",
caption="Fuente: Open Street Map",
y="Escuelas",
x="Barrios")