library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------------------------------------ tidyverse 1.3.0 --
## v ggplot2 3.3.0 v purrr 0.3.4
## v tibble 3.0.1 v dplyr 0.8.5
## v tidyr 1.0.2 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts --------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
Bibliotecas_Publicas_OK <- st_read("http://bostonopendata-boston.opendata.arcgis.com/datasets/cb00f9248aa6404ab741071ca3806c0e_6.geojson?outSR={%22latestWkid%22:2249,%22wkid%22:102686}")
## Reading layer `f34727f8-abe9-4f9f-9248-ae2c770fd4402020410-1-1ox0hsn.adz7' from data source `http://bostonopendata-boston.opendata.arcgis.com/datasets/cb00f9248aa6404ab741071ca3806c0e_6.geojson?outSR={%22latestWkid%22:2249,%22wkid%22:102686}' using driver `GeoJSON'
## Simple feature collection with 26 features and 7 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -71.16788 ymin: 42.2571 xmax: -71.02858 ymax: 42.37777
## CRS: 4326
Bibliotecas_Publicas_Ordenadas <- Bibliotecas_Publicas_OK %>%
select(LIBRARIES_, DISTRICT, ST_ADDRESS, BRANCH, geometry)
head(Bibliotecas_Publicas_Ordenadas)
## Simple feature collection with 6 features and 4 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -71.12214 ymin: 42.2571 xmax: -71.06501 ymax: 42.36145
## CRS: 4326
## LIBRARIES_ DISTRICT ST_ADDRESS BRANCH
## 1 5 151 Cambridge St West End
## 2 11 690 Washington St Codman Square
## 3 17 HYDE PARK 35 Harvard Av Hyde Park
## 4 18 JAMAICA PLAIN 433 Centre St Connolly
## 5 22 ROXBURY 149 Dudley St Dudley
## 6 1 COPLEY SQUARE 700 Boylston St Central
## geometry
## 1 POINT (-71.06501 42.36145)
## 2 POINT (-71.07097 42.28752)
## 3 POINT (-71.12214 42.2571)
## 4 POINT (-71.1111 42.32065)
## 5 POINT (-71.08385 42.32849)
## 6 POINT (-71.07887 42.34944)
Barrios_Boston_OK <- st_read("Boston_Neighborhoods.shp")
## Reading layer `Boston_Neighborhoods' from data source `C:\Users\ELEONORA\Documents\utdt\CIENCIA DE DATOS PARA CIUDADES\DATOS 2\CLASE1_DATOS 2\TP1_DATOS2\Boston_Neighborhoods.shp' using driver `ESRI Shapefile'
## Simple feature collection with 26 features and 7 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 739715.3 ymin: 2908294 xmax: 812255.1 ymax: 2970086
## proj4string: +proj=lcc +lat_1=41.71666666666667 +lat_2=42.68333333333333 +lat_0=41 +lon_0=-71.5 +x_0=200000 +y_0=750000.0000000001 +datum=NAD83 +units=us-ft +no_defs
head (Barrios_Boston_OK)
## Simple feature collection with 6 features and 7 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 751472.1 ymin: 2922750 xmax: 776215.4 ymax: 2953742
## proj4string: +proj=lcc +lat_1=41.71666666666667 +lat_2=42.68333333333333 +lat_0=41 +lon_0=-71.5 +x_0=200000 +y_0=750000.0000000001 +datum=NAD83 +units=us-ft +no_defs
## OBJECTID Name Acres Neighborho SqMiles ShapeSTAre
## 1 27 Roslindale 1605.56824 15 2.51 69938272.9
## 2 28 Jamaica Plain 2519.24539 11 3.94 109737890.8
## 3 29 Mission Hill 350.85356 13 0.55 15283120.0
## 4 30 Longwood 188.61195 28 0.29 8215903.5
## 5 31 Bay Village 26.53984 33 0.04 1156070.8
## 6 32 Leather District 15.63991 27 0.02 681271.7
## ShapeSTLen geometry
## 1 53563.913 MULTIPOLYGON (((757409.1 29...
## 2 56349.937 MULTIPOLYGON (((762983.8 29...
## 3 17918.724 MULTIPOLYGON (((766903.6 29...
## 4 11908.757 MULTIPOLYGON (((764826.9 29...
## 5 4650.635 MULTIPOLYGON (((773315.7 29...
## 6 3237.141 MULTIPOLYGON (((775544.1 29...
names(Barrios_Boston_OK)
## [1] "OBJECTID" "Name" "Acres" "Neighborho" "SqMiles"
## [6] "ShapeSTAre" "ShapeSTLen" "geometry"
plot(Barrios_Boston_OK)
ggplot() +
geom_sf(data = Barrios_Boston_OK) +
geom_sf(data = Bibliotecas_Publicas_Ordenadas,
color = "red")
#### Observamos bibliotecas asignadas a los distintos barrios.
st_crs(Barrios_Boston_OK)
## Coordinate Reference System:
## No user input
## wkt:
## PROJCS["NAD_1983_StatePlane_Massachusetts_Mainland_FIPS_2001_Feet",
## GEOGCS["GCS_North_American_1983",
## DATUM["North_American_Datum_1983",
## SPHEROID["GRS_1980",6378137.0,298.257222101]],
## PRIMEM["Greenwich",0.0],
## UNIT["Degree",0.017453292519943295],
## AUTHORITY["EPSG","4269"]],
## PROJECTION["Lambert_Conformal_Conic_2SP"],
## PARAMETER["False_Easting",656166.6666666665],
## PARAMETER["False_Northing",2460625.0],
## PARAMETER["Central_Meridian",-71.5],
## PARAMETER["Standard_Parallel_1",41.71666666666667],
## PARAMETER["Standard_Parallel_2",42.68333333333333],
## PARAMETER["Latitude_Of_Origin",41.0],
## UNIT["Foot_US",0.30480060960121924]]
st_crs(Bibliotecas_Publicas_Ordenadas)
## Coordinate Reference System:
## User input: 4326
## wkt:
## GEOGCS["WGS 84",
## DATUM["WGS_1984",
## SPHEROID["WGS 84",6378137,298.257223563,
## AUTHORITY["EPSG","7030"]],
## AUTHORITY["EPSG","6326"]],
## PRIMEM["Greenwich",0,
## AUTHORITY["EPSG","8901"]],
## UNIT["degree",0.0174532925199433,
## AUTHORITY["EPSG","9122"]],
## AUTHORITY["EPSG","4326"]]
Barrios_Boston_OK <- st_transform(Barrios_Boston_OK, 4326)
st_crs(Barrios_Boston_OK)
## Coordinate Reference System:
## User input: EPSG:4326
## wkt:
## GEOGCS["WGS 84",
## DATUM["WGS_1984",
## SPHEROID["WGS 84",6378137,298.257223563,
## AUTHORITY["EPSG","7030"]],
## AUTHORITY["EPSG","6326"]],
## PRIMEM["Greenwich",0,
## AUTHORITY["EPSG","8901"]],
## UNIT["degree",0.0174532925199433,
## AUTHORITY["EPSG","9122"]],
## AUTHORITY["EPSG","4326"]]
head(Bibliotecas_Publicas_Ordenadas)
## Simple feature collection with 6 features and 4 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -71.12214 ymin: 42.2571 xmax: -71.06501 ymax: 42.36145
## CRS: 4326
## LIBRARIES_ DISTRICT ST_ADDRESS BRANCH
## 1 5 151 Cambridge St West End
## 2 11 690 Washington St Codman Square
## 3 17 HYDE PARK 35 Harvard Av Hyde Park
## 4 18 JAMAICA PLAIN 433 Centre St Connolly
## 5 22 ROXBURY 149 Dudley St Dudley
## 6 1 COPLEY SQUARE 700 Boylston St Central
## geometry
## 1 POINT (-71.06501 42.36145)
## 2 POINT (-71.07097 42.28752)
## 3 POINT (-71.12214 42.2571)
## 4 POINT (-71.1111 42.32065)
## 5 POINT (-71.08385 42.32849)
## 6 POINT (-71.07887 42.34944)
head(Barrios_Boston_OK)
## Simple feature collection with 6 features and 7 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -71.14779 ymin: 42.26761 xmax: -71.05588 ymax: 42.35237
## CRS: EPSG:4326
## OBJECTID Name Acres Neighborho SqMiles ShapeSTAre
## 1 27 Roslindale 1605.56824 15 2.51 69938272.9
## 2 28 Jamaica Plain 2519.24539 11 3.94 109737890.8
## 3 29 Mission Hill 350.85356 13 0.55 15283120.0
## 4 30 Longwood 188.61195 28 0.29 8215903.5
## 5 31 Bay Village 26.53984 33 0.04 1156070.8
## 6 32 Leather District 15.63991 27 0.02 681271.7
## ShapeSTLen geometry
## 1 53563.913 MULTIPOLYGON (((-71.12593 4...
## 2 56349.937 MULTIPOLYGON (((-71.10499 4...
## 3 17918.724 MULTIPOLYGON (((-71.09043 4...
## 4 11908.757 MULTIPOLYGON (((-71.09811 4...
## 5 4650.635 MULTIPOLYGON (((-71.06663 4...
## 6 3237.141 MULTIPOLYGON (((-71.05838 4...
Bibliotecas_Publicas_Ordenadas_con_Barrios <- st_join(Bibliotecas_Publicas_Ordenadas, Barrios_Boston_OK)
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
head(Bibliotecas_Publicas_Ordenadas_con_Barrios)
## Simple feature collection with 6 features and 11 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -71.12214 ymin: 42.2571 xmax: -71.06501 ymax: 42.36145
## CRS: 4326
## LIBRARIES_ DISTRICT ST_ADDRESS BRANCH OBJECTID
## 1 5 151 Cambridge St West End 40
## 2 11 690 Washington St Codman Square 48
## 3 17 HYDE PARK 35 Harvard Av Hyde Park 46
## 4 18 JAMAICA PLAIN 433 Centre St Connolly 28
## 5 22 ROXBURY 149 Dudley St Dudley 35
## 6 1 COPLEY SQUARE 700 Boylston St Central 37
## Name Acres Neighborho SqMiles ShapeSTAre ShapeSTLen
## 1 West End 190.4907 31 0.30 8297743 17728.59
## 2 Dorchester 4662.8795 6 7.29 203114217 104344.03
## 3 Hyde Park 2927.2212 10 4.57 127509243 66861.24
## 4 Jamaica Plain 2519.2454 11 3.94 109737891 56349.94
## 5 Roxbury 2108.4691 16 3.29 91844546 49488.80
## 6 Back Bay 399.3144 2 0.62 17394066 19455.67
## geometry
## 1 POINT (-71.06501 42.36145)
## 2 POINT (-71.07097 42.28752)
## 3 POINT (-71.12214 42.2571)
## 4 POINT (-71.1111 42.32065)
## 5 POINT (-71.08385 42.32849)
## 6 POINT (-71.07887 42.34944)
names(Bibliotecas_Publicas_Ordenadas_con_Barrios)
## [1] "LIBRARIES_" "DISTRICT" "ST_ADDRESS" "BRANCH" "OBJECTID"
## [6] "Name" "Acres" "Neighborho" "SqMiles" "ShapeSTAre"
## [11] "ShapeSTLen" "geometry"
Total_Bibliotecas_Publicas_Ordenadas_con_Barrios <- Bibliotecas_Publicas_Ordenadas_con_Barrios %>%
group_by(Neighborho) %>%
summarise(total=n())
Total_Bibliotecas_Publicas_Ordenadas_con_Barrios
## Simple feature collection with 18 features and 2 fields
## geometry type: GEOMETRY
## dimension: XY
## bbox: xmin: -71.16788 ymin: 42.2571 xmax: -71.02858 ymax: 42.37777
## CRS: 4326
## # A tibble: 18 x 3
## Neighborho total geometry
## <fct> <int> <GEOMETRY [°]>
## 1 10 1 POINT (-71.12214 42.2571)
## 2 11 2 MULTIPOINT ((-71.11488 42.30863), (-71.1111 42.32065))
## 3 12 2 MULTIPOINT ((-71.09314 42.27752), (-71.06851 42.27355))
## 4 13 1 POINT (-71.09908 42.33246)
## 5 14 1 POINT (-71.05502 42.36408)
## 6 15 1 POINT (-71.12865 42.28565)
## 7 16 2 MULTIPOINT ((-71.09563 42.31406), (-71.08385 42.32849))
## 8 17 1 POINT (-71.03872 42.33584)
## 9 19 1 POINT (-71.15745 42.2834)
## 10 2 1 POINT (-71.07887 42.34944)
## 11 24 1 POINT (-71.12811 42.36011)
## 12 25 2 MULTIPOINT ((-71.16788 42.35132), (-71.15291 42.34769))
## 13 31 1 POINT (-71.06501 42.36145)
## 14 32 1 POINT (-71.07703 42.34133)
## 15 4 1 POINT (-71.06444 42.37582)
## 16 6 5 MULTIPOINT ((-71.08119 42.30799), (-71.07097 42.28752), (-7~
## 17 7 1 POINT (-71.06306 42.35214)
## 18 8 1 POINT (-71.02858 42.37777)
ggplot(Total_Bibliotecas_Publicas_Ordenadas_con_Barrios) +
geom_point(aes(x=Neighborho, y=total), color="red") +
labs(title="Bibliotecas por Barrio",
subtitle="Boston",
caption="Fuente:https://data.boston.gov/dataset/public-libraries/resource/ed710c4a-689a-47af-9dd0-6e4215003c24",
x="Barrio ID",
y="Total")
ggplot() +
geom_sf(data = Barrios_Boston_OK) +
geom_sf(data = Total_Bibliotecas_Publicas_Ordenadas_con_Barrios, aes(color = Neighborho), size=1) +
theme(legend.position = "bottom")
Total_Bibliotecas_Publicas_Ordenadas_con_Barrios <- Total_Bibliotecas_Publicas_Ordenadas_con_Barrios %>%
st_set_geometry(NULL)
head(Total_Bibliotecas_Publicas_Ordenadas_con_Barrios)
## # A tibble: 6 x 2
## Neighborho total
## <fct> <int>
## 1 10 1
## 2 11 2
## 3 12 2
## 4 13 1
## 5 14 1
## 6 15 1
colnames(Total_Bibliotecas_Publicas_Ordenadas_con_Barrios)[colnames(Total_Bibliotecas_Publicas_Ordenadas_con_Barrios) %in% c('total')] <- paste0(c('Total_Bibliotecas_Publicas'))
head(Total_Bibliotecas_Publicas_Ordenadas_con_Barrios)
## # A tibble: 6 x 2
## Neighborho Total_Bibliotecas_Publicas
## <fct> <int>
## 1 10 1
## 2 11 2
## 3 12 2
## 4 13 1
## 5 14 1
## 6 15 1
Bibliotecas_Barrios_Total <- Barrios_Boston_OK %>%
left_join(Total_Bibliotecas_Publicas_Ordenadas_con_Barrios, by="Neighborho")
head(Bibliotecas_Barrios_Total)
## Simple feature collection with 6 features and 8 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -71.14779 ymin: 42.26761 xmax: -71.05588 ymax: 42.35237
## CRS: EPSG:4326
## OBJECTID Name Acres Neighborho SqMiles ShapeSTAre
## 1 27 Roslindale 1605.56824 15 2.51 69938272.9
## 2 28 Jamaica Plain 2519.24539 11 3.94 109737890.8
## 3 29 Mission Hill 350.85356 13 0.55 15283120.0
## 4 30 Longwood 188.61195 28 0.29 8215903.5
## 5 31 Bay Village 26.53984 33 0.04 1156070.8
## 6 32 Leather District 15.63991 27 0.02 681271.7
## ShapeSTLen Total_Bibliotecas_Publicas geometry
## 1 53563.913 1 MULTIPOLYGON (((-71.12593 4...
## 2 56349.937 2 MULTIPOLYGON (((-71.10499 4...
## 3 17918.724 1 MULTIPOLYGON (((-71.09043 4...
## 4 11908.757 NA MULTIPOLYGON (((-71.09811 4...
## 5 4650.635 NA MULTIPOLYGON (((-71.06663 4...
## 6 3237.141 NA MULTIPOLYGON (((-71.05838 4...
names(Bibliotecas_Barrios_Total)
## [1] "OBJECTID" "Name"
## [3] "Acres" "Neighborho"
## [5] "SqMiles" "ShapeSTAre"
## [7] "ShapeSTLen" "Total_Bibliotecas_Publicas"
## [9] "geometry"
ggplot() +
geom_sf(data = Bibliotecas_Barrios_Total, aes(fill = Total_Bibliotecas_Publicas)) +
geom_sf_text(data = Bibliotecas_Barrios_Total, aes(label = Name), size=2, colour="black") +
labs(title = "Total Bibliotecas por Barrio",
subtitle = "Boston",
fill = " Total Bibliotecas",
caption= "Fuente: https://data.boston.gov/dataset/boston-neighborhoods y https://data.boston.gov/dataset/public-libraries/resource/ed710c4a-689a-47af-9dd0-6e4215003c24",
y="",
x="") +
scale_fill_gradient(low="yellow", high="red")
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
library(sf)
library(tidyverse)
library(osmdata)
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(leaflet)
devtools::install_github("yutannihilation/ggsflabel")
## WARNING: Rtools is required to build R packages, but is not currently installed.
##
## Please download and install Rtools custom from http://cran.r-project.org/bin/windows/Rtools/.
## Skipping install of 'ggsflabel' from a github remote, the SHA1 (a489481b) has not changed since last install.
## Use `force = TRUE` to force installation
library(ggsflabel)
##
## Attaching package: 'ggsflabel'
## The following objects are masked from 'package:ggplot2':
##
## geom_sf_label, geom_sf_text, StatSfCoordinates
bbox_Boston <- getbb("Boston, Massachusetts")
bbox_Boston
## min max
## x -71.19125 -70.80449
## y 42.22791 42.39698
bbox_poly_Boston <- getbb("Boston, Massachusetts", format_out = "sf_polygon")
leaflet(bbox_poly_Boston) %>%
addTiles() %>%
addPolygons()
#### Efectivamente se corresponde con la Ciudad de Boston que veníamos trabajando.
Boston_Calles <- opq(bbox_Boston) %>%
add_osm_feature(key = "highway") %>%
osmdata_sf()
Boston_Calles <- st_intersection(Boston_Calles$osm_lines, bbox_poly_Boston)
## although coordinates are longitude/latitude, st_intersection assumes that they are planar
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
ggplot() +
geom_sf(data = Boston_Calles)
Boston_Calles <- Boston_Calles %>%
mutate(maxspeed = as.numeric(str_remove(maxspeed, "mph")),
lanes = ifelse(is.na(lanes), 1, as.numeric(lanes)))
## Warning: NAs introduced by coercion
ggplot(Boston_Calles) +
geom_sf(aes(color = maxspeed), alpha = 0.5) +
scale_color_viridis_c() +
theme_void() +
labs(title = "Boston, Massachusetts",
subtitle = "Vías de circulación",
caption = "fuente: OpenStreetMap",
color = "velocidad máxima")
####Ahora descargaremos una capa de datos de tipo puntos, serán las Bibliotecas = Library, que no son solo las públicas, sino que incluye todas las bibliotecas donde se pueden tomar prestados libros en la ciudad.
Boston_Biliotecas_OSM <- opq(bbox_Boston) %>%
add_osm_feature(key = "amenity", value = "library") %>%
osmdata_sf()
####Y también eliminamos las bibliotecas que estan afuera de la ciudad:
Boston_Biliotecas_OSM <- st_intersection(Boston_Biliotecas_OSM$osm_points, bbox_poly_Boston)
## although coordinates are longitude/latitude, st_intersection assumes that they are planar
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
ggplot() +
geom_sf(data = Boston_Calles,
color = "gray48") +
geom_sf(data = Boston_Biliotecas_OSM,
color = "red", alpha = .1) +
labs(title = "Bibliotecas",
subtitle = "Boston",
caption = "Fuente: OpenStreetMap")
### Conclusion #### Podemos observar, a diferencia del grafico realizado en el TP1 , pareceria que hay una mayor concentracion de bibliotecas hacia el norte de la ciudad. Esto puede deberse a que el dataset del grafico anterior solo mostraba “bibliotecas Publicas”, mientras que este incorpora a estas las bibliotecas privadas que deben estar localizadas en la zona Norte, porque en el resto de la ciudad se ve una distribucion bastante pareciada al grafico anterior.Igual lo verificaremos mas adelante con el grafico de Coropletas.
####Para ello primero realizamos un join espacial de los Barrios de Boston con las Bibliotecas OSM utilizando el comando st_join:
Bibliotecas_OSM_en_Barrios <- st_join(Boston_Biliotecas_OSM, Barrios_Boston_OK)
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
names(Bibliotecas_OSM_en_Barrios)
## [1] "osm_id" "name" "addr.city"
## [4] "addr.housenumber" "addr.place" "addr.postcode"
## [7] "addr.state" "addr.street" "addr.unit"
## [10] "alt_name" "amenity" "attribution"
## [13] "building" "created_by" "designation"
## [16] "ele" "email" "entrance"
## [19] "gnis.county_name" "gnis.feature_id" "gnis.import_uuid"
## [22] "gnis.reviewed" "is_in" "level"
## [25] "library" "note" "office"
## [28] "opening_hours" "operator" "operator.type"
## [31] "phone" "postal_code" "ref.isil"
## [34] "region" "source" "source.url"
## [37] "source_url" "toilets" "toilets.wheelchair"
## [40] "type" "website" "wheelchair"
## [43] "wheelchair.description" "wifi" "wikidata"
## [46] "wikipedia" "OBJECTID" "Name"
## [49] "Acres" "Neighborho" "SqMiles"
## [52] "ShapeSTAre" "ShapeSTLen" "geometry"
Total_Bibliotecas_OSM_en_Barrios <- Bibliotecas_OSM_en_Barrios %>%
group_by(Neighborho) %>%
summarise(total=n())
Total_Bibliotecas_OSM_en_Barrios
## Simple feature collection with 23 features and 2 fields
## geometry type: GEOMETRY
## dimension: XY
## bbox: xmin: -71.17893 ymin: 42.23389 xmax: -71.02771 ymax: 42.38127
## CRS: EPSG:4326
## # A tibble: 23 x 3
## Neighborho total geometry
## <fct> <int> <GEOMETRY [°]>
## 1 10 2 MULTIPOINT ((-71.13317 42.23389), (-71.11774 42.26296))
## 2 11 18 MULTIPOINT ((-71.12824 42.30121), (-71.12094 42.30745), (-7~
## 3 12 20 MULTIPOINT ((-71.09324 42.27703), (-71.09323 42.27745), (-7~
## 4 13 2 MULTIPOINT ((-71.10667 42.33007), (-71.09556 42.3356))
## 5 14 3 MULTIPOINT ((-71.05499 42.36401), (-71.05477 42.36649), (-7~
## 6 16 28 MULTIPOINT ((-71.0919 42.33246), (-71.08593 42.3168), (-71.~
## 7 17 1 POINT (-71.07094 42.33292)
## 8 19 4 MULTIPOINT ((-71.17893 42.27802), (-71.17454 42.28218), (-7~
## 9 2 53 MULTIPOINT ((-71.08714 42.34664), (-71.08616 42.34512), (-7~
## 10 24 52 MULTIPOINT ((-71.12839 42.36023), (-71.12825 42.36033), (-7~
## # ... with 13 more rows
####Le quitamos la geometría al dataset generado
Total_Bibliotecas_OSM_en_Barrios <- Total_Bibliotecas_OSM_en_Barrios %>%
st_set_geometry(NULL)
head(Total_Bibliotecas_OSM_en_Barrios)
## # A tibble: 6 x 2
## Neighborho total
## <fct> <int>
## 1 10 2
## 2 11 18
## 3 12 20
## 4 13 2
## 5 14 3
## 6 16 28
Total_Bibliotecas_OSM_en_Barrios <- Barrios_Boston_OK %>%
left_join(Total_Bibliotecas_OSM_en_Barrios, by="Neighborho")
head(Total_Bibliotecas_OSM_en_Barrios)
## Simple feature collection with 6 features and 8 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -71.14779 ymin: 42.26761 xmax: -71.05588 ymax: 42.35237
## CRS: EPSG:4326
## OBJECTID Name Acres Neighborho SqMiles ShapeSTAre
## 1 27 Roslindale 1605.56824 15 2.51 69938272.9
## 2 28 Jamaica Plain 2519.24539 11 3.94 109737890.8
## 3 29 Mission Hill 350.85356 13 0.55 15283120.0
## 4 30 Longwood 188.61195 28 0.29 8215903.5
## 5 31 Bay Village 26.53984 33 0.04 1156070.8
## 6 32 Leather District 15.63991 27 0.02 681271.7
## ShapeSTLen total geometry
## 1 53563.913 NA MULTIPOLYGON (((-71.12593 4...
## 2 56349.937 18 MULTIPOLYGON (((-71.10499 4...
## 3 17918.724 2 MULTIPOLYGON (((-71.09043 4...
## 4 11908.757 72 MULTIPOLYGON (((-71.09811 4...
## 5 4650.635 2 MULTIPOLYGON (((-71.06663 4...
## 6 3237.141 NA MULTIPOLYGON (((-71.05838 4...
ggplot() +
geom_sf(data = Total_Bibliotecas_OSM_en_Barrios, aes(fill = total)) +
geom_sf_text(data = Total_Bibliotecas_OSM_en_Barrios, aes(label = Name), size = 2, colour = "black") +
labs(title = "Total Bibliotecas por Barrio",
subtitle = "Boston",
fill = "Total Bibliotecas",
caption= "Fuente: https://data.boston.gov/dataset/boston-neighborhoods y
OpenStreetMap",
y="",
x="") +
scale_fill_gradient(low="yellow", high="red")
## Warning in st_point_on_surface.sfc(data$geometry): st_point_on_surface may not
## give correct results for longitude/latitude data
ggplot(Total_Bibliotecas_OSM_en_Barrios) +
geom_point(aes(x=Neighborho, y=total), color="red") +
labs(title="Bibliotecas por Barrio",
subtitle="Boston",
caption="OpenStreetMap",
x="Barrio ID",
y="Total")
## Warning: Removed 3 rows containing missing values (geom_point).
ggplot() +
geom_sf(data = Total_Bibliotecas_OSM_en_Barrios, aes(fill = total/Acres)) +
geom_sf_text(data = Total_Bibliotecas_OSM_en_Barrios, aes(label = Name), size = 2, colour = "black") +
labs(title = "Total Bibliotecas por Barrio",
subtitle = "Boston",
fill = "Total Bibliotecas",
caption= "Fuente: https://data.boston.gov/dataset/boston-neighborhoods y
OpenStreetMap",
y="",
x="") +
scale_fill_gradient(low="yellow", high="red")
## Warning in st_point_on_surface.sfc(data$geometry): st_point_on_surface may not
## give correct results for longitude/latitude data
library(rtweet)
##
## Attaching package: 'rtweet'
## The following object is masked from 'package:purrr':
##
## flatten
Tweets_Boston <- read_csv("tweets_boston.csv")
## Parsed with column specification:
## cols(
## .default = col_character(),
## created_at = col_datetime(format = ""),
## display_text_width = col_double(),
## is_quote = col_logical(),
## is_retweet = col_logical(),
## favorite_count = col_double(),
## retweet_count = col_double(),
## quote_count = col_logical(),
## reply_count = col_logical(),
## ext_media_type = col_logical(),
## quoted_created_at = col_datetime(format = ""),
## quoted_favorite_count = col_double(),
## quoted_retweet_count = col_double(),
## quoted_followers_count = col_double(),
## quoted_friends_count = col_double(),
## quoted_statuses_count = col_double(),
## quoted_verified = col_logical(),
## retweet_status_id = col_logical(),
## retweet_text = col_logical(),
## retweet_created_at = col_logical(),
## retweet_source = col_logical()
## # ... with 20 more columns
## )
## See spec(...) for full column specifications.
Tweets_Boston
## # A tibble: 17,696 x 90
## user_id status_id created_at screen_name text source
## <chr> <chr> <dttm> <chr> <chr> <chr>
## 1 x30290~ x1258816~ 2020-05-08 17:50:31 shitoewigk~ i ju~ Twitt~
## 2 x30290~ x1258807~ 2020-05-08 17:12:38 shitoewigk~ @fir~ Twitt~
## 3 x30290~ x1258810~ 2020-05-08 17:24:32 shitoewigk~ oh m~ Twitt~
## 4 x30290~ x1258814~ 2020-05-08 17:40:39 shitoewigk~ yeah~ Twitt~
## 5 x30290~ x1258809~ 2020-05-08 17:23:46 shitoewigk~ nana~ Twitt~
## 6 x30290~ x1258790~ 2020-05-08 16:06:12 shitoewigk~ the ~ Twitt~
## 7 x30290~ x1258789~ 2020-05-08 16:03:29 shitoewigk~ i so~ Twitt~
## 8 x30290~ x1258813~ 2020-05-08 17:38:03 shitoewigk~ reme~ Twitt~
## 9 x23460~ x1258816~ 2020-05-08 17:50:30 M_Lambert4~ ACLU~ Twitt~
## 10 x23329~ x1258802~ 2020-05-08 16:55:48 itsbigsuper I ai~ Twitt~
## # ... with 17,686 more rows, and 84 more variables: display_text_width <dbl>,
## # reply_to_status_id <chr>, reply_to_user_id <chr>,
## # reply_to_screen_name <chr>, is_quote <lgl>, is_retweet <lgl>,
## # favorite_count <dbl>, retweet_count <dbl>, quote_count <lgl>,
## # reply_count <lgl>, hashtags <chr>, symbols <chr>, urls_url <chr>,
## # urls_t.co <chr>, urls_expanded_url <chr>, media_url <chr>,
## # media_t.co <chr>, media_expanded_url <chr>, media_type <chr>,
## # ext_media_url <chr>, ext_media_t.co <chr>, ext_media_expanded_url <chr>,
## # ext_media_type <lgl>, mentions_user_id <chr>, mentions_screen_name <chr>,
## # lang <chr>, quoted_status_id <chr>, quoted_text <chr>,
## # quoted_created_at <dttm>, quoted_source <chr>, quoted_favorite_count <dbl>,
## # quoted_retweet_count <dbl>, quoted_user_id <chr>, quoted_screen_name <chr>,
## # quoted_name <chr>, quoted_followers_count <dbl>,
## # quoted_friends_count <dbl>, quoted_statuses_count <dbl>,
## # quoted_location <chr>, quoted_description <chr>, quoted_verified <lgl>,
## # retweet_status_id <lgl>, retweet_text <lgl>, retweet_created_at <lgl>,
## # retweet_source <lgl>, retweet_favorite_count <lgl>,
## # retweet_retweet_count <lgl>, retweet_user_id <lgl>,
## # retweet_screen_name <lgl>, retweet_name <lgl>,
## # retweet_followers_count <lgl>, retweet_friends_count <lgl>,
## # retweet_statuses_count <lgl>, retweet_location <lgl>,
## # retweet_description <lgl>, retweet_verified <lgl>, place_url <chr>,
## # place_name <chr>, place_full_name <chr>, place_type <chr>, country <chr>,
## # country_code <chr>, geo_coords <chr>, coords_coords <chr>,
## # bbox_coords <chr>, status_url <chr>, name <chr>, location <chr>,
## # description <chr>, url <chr>, protected <lgl>, followers_count <dbl>,
## # friends_count <dbl>, listed_count <dbl>, statuses_count <dbl>,
## # favourites_count <dbl>, account_created_at <dttm>, verified <lgl>,
## # profile_url <chr>, profile_expanded_url <chr>, account_lang <lgl>,
## # profile_banner_url <chr>, profile_background_url <chr>,
## # profile_image_url <chr>
names(Tweets_Boston)
## [1] "user_id" "status_id"
## [3] "created_at" "screen_name"
## [5] "text" "source"
## [7] "display_text_width" "reply_to_status_id"
## [9] "reply_to_user_id" "reply_to_screen_name"
## [11] "is_quote" "is_retweet"
## [13] "favorite_count" "retweet_count"
## [15] "quote_count" "reply_count"
## [17] "hashtags" "symbols"
## [19] "urls_url" "urls_t.co"
## [21] "urls_expanded_url" "media_url"
## [23] "media_t.co" "media_expanded_url"
## [25] "media_type" "ext_media_url"
## [27] "ext_media_t.co" "ext_media_expanded_url"
## [29] "ext_media_type" "mentions_user_id"
## [31] "mentions_screen_name" "lang"
## [33] "quoted_status_id" "quoted_text"
## [35] "quoted_created_at" "quoted_source"
## [37] "quoted_favorite_count" "quoted_retweet_count"
## [39] "quoted_user_id" "quoted_screen_name"
## [41] "quoted_name" "quoted_followers_count"
## [43] "quoted_friends_count" "quoted_statuses_count"
## [45] "quoted_location" "quoted_description"
## [47] "quoted_verified" "retweet_status_id"
## [49] "retweet_text" "retweet_created_at"
## [51] "retweet_source" "retweet_favorite_count"
## [53] "retweet_retweet_count" "retweet_user_id"
## [55] "retweet_screen_name" "retweet_name"
## [57] "retweet_followers_count" "retweet_friends_count"
## [59] "retweet_statuses_count" "retweet_location"
## [61] "retweet_description" "retweet_verified"
## [63] "place_url" "place_name"
## [65] "place_full_name" "place_type"
## [67] "country" "country_code"
## [69] "geo_coords" "coords_coords"
## [71] "bbox_coords" "status_url"
## [73] "name" "location"
## [75] "description" "url"
## [77] "protected" "followers_count"
## [79] "friends_count" "listed_count"
## [81] "statuses_count" "favourites_count"
## [83] "account_created_at" "verified"
## [85] "profile_url" "profile_expanded_url"
## [87] "account_lang" "profile_banner_url"
## [89] "profile_background_url" "profile_image_url"
head(Tweets_Boston)
## # A tibble: 6 x 90
## user_id status_id created_at screen_name text source
## <chr> <chr> <dttm> <chr> <chr> <chr>
## 1 x30290~ x1258816~ 2020-05-08 17:50:31 shitoewigk~ i ju~ Twitt~
## 2 x30290~ x1258807~ 2020-05-08 17:12:38 shitoewigk~ @fir~ Twitt~
## 3 x30290~ x1258810~ 2020-05-08 17:24:32 shitoewigk~ oh m~ Twitt~
## 4 x30290~ x1258814~ 2020-05-08 17:40:39 shitoewigk~ yeah~ Twitt~
## 5 x30290~ x1258809~ 2020-05-08 17:23:46 shitoewigk~ nana~ Twitt~
## 6 x30290~ x1258790~ 2020-05-08 16:06:12 shitoewigk~ the ~ Twitt~
## # ... with 84 more variables: display_text_width <dbl>,
## # reply_to_status_id <chr>, reply_to_user_id <chr>,
## # reply_to_screen_name <chr>, is_quote <lgl>, is_retweet <lgl>,
## # favorite_count <dbl>, retweet_count <dbl>, quote_count <lgl>,
## # reply_count <lgl>, hashtags <chr>, symbols <chr>, urls_url <chr>,
## # urls_t.co <chr>, urls_expanded_url <chr>, media_url <chr>,
## # media_t.co <chr>, media_expanded_url <chr>, media_type <chr>,
## # ext_media_url <chr>, ext_media_t.co <chr>, ext_media_expanded_url <chr>,
## # ext_media_type <lgl>, mentions_user_id <chr>, mentions_screen_name <chr>,
## # lang <chr>, quoted_status_id <chr>, quoted_text <chr>,
## # quoted_created_at <dttm>, quoted_source <chr>, quoted_favorite_count <dbl>,
## # quoted_retweet_count <dbl>, quoted_user_id <chr>, quoted_screen_name <chr>,
## # quoted_name <chr>, quoted_followers_count <dbl>,
## # quoted_friends_count <dbl>, quoted_statuses_count <dbl>,
## # quoted_location <chr>, quoted_description <chr>, quoted_verified <lgl>,
## # retweet_status_id <lgl>, retweet_text <lgl>, retweet_created_at <lgl>,
## # retweet_source <lgl>, retweet_favorite_count <lgl>,
## # retweet_retweet_count <lgl>, retweet_user_id <lgl>,
## # retweet_screen_name <lgl>, retweet_name <lgl>,
## # retweet_followers_count <lgl>, retweet_friends_count <lgl>,
## # retweet_statuses_count <lgl>, retweet_location <lgl>,
## # retweet_description <lgl>, retweet_verified <lgl>, place_url <chr>,
## # place_name <chr>, place_full_name <chr>, place_type <chr>, country <chr>,
## # country_code <chr>, geo_coords <chr>, coords_coords <chr>,
## # bbox_coords <chr>, status_url <chr>, name <chr>, location <chr>,
## # description <chr>, url <chr>, protected <lgl>, followers_count <dbl>,
## # friends_count <dbl>, listed_count <dbl>, statuses_count <dbl>,
## # favourites_count <dbl>, account_created_at <dttm>, verified <lgl>,
## # profile_url <chr>, profile_expanded_url <chr>, account_lang <lgl>,
## # profile_banner_url <chr>, profile_background_url <chr>,
## # profile_image_url <chr>
options(scipen = 20)
ggplot(filter(Tweets_Boston, !is_retweet))+
geom_histogram(aes(x = retweet_count))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Tweets_Boston %>%
top_n(6, retweet_count) %>%
arrange(desc(retweet_count)) %>%
select(screen_name, retweet_count, followers_count, text, location)
## # A tibble: 6 x 5
## screen_name retweet_count followers_count text location
## <chr> <dbl> <dbl> <chr> <chr>
## 1 jujuboston 255 289670 "I am so GRATEFUL for th~ Boston, ~
## 2 ValaAfshar 138 457783 "This is the attitude an~ Boston
## 3 jasonfurman 98 47447 "Will Sweden's strategy ~ Cambridg~
## 4 ValaAfshar 95 457783 "We love our parents bec~ Boston
## 5 TeamAlbania~ 71 22984 "An interactial couple i~ Boston, ~
## 6 FieldYates 70 526231 "The Bills have inked th~ Boston, ~
ts_plot(Tweets_Boston, "minutes")
ggplot(Tweets_Boston) +
geom_histogram(aes(x = followers_count))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Tweets_Boston %>%
top_n(5, followers_count) %>%
arrange(desc(followers_count)) %>%
select(screen_name, followers_count, location)
## # A tibble: 5 x 3
## screen_name followers_count location
## <chr> <dbl> <chr>
## 1 HarvardBiz 5581038 Boston, MA
## 2 HarvardBiz 5581038 Boston, MA
## 3 HarvardBiz 5581038 Boston, MA
## 4 HarvardHealth 2458514 Boston, MA
## 5 RedSox 2137053 Fenway Park
Tweets_Boston %>%
top_n(5, followers_count) %>%
arrange(desc(followers_count)) %>%
select(screen_name, followers_count, location, text)
## # A tibble: 5 x 4
## screen_name followers_count location text
## <chr> <dbl> <chr> <chr>
## 1 HarvardBiz 5581038 Boston, MA "Not all family business leaders are ~
## 2 HarvardBiz 5581038 Boston, MA "How Nasdaq evolved into a global tec~
## 3 HarvardBiz 5581038 Boston, MA "Over time, your company intranet may~
## 4 HarvardHeal~ 2458514 Boston, MA "Taking blood pressure pills at bedti~
## 5 RedSox 2137053 Fenway Pa~ "If you want to get J.D. on the dance~
Tweets_Boston %>%
top_n(10, followers_count) %>%
arrange(desc(followers_count)) %>%
select(screen_name, followers_count, location, text)
## # A tibble: 20 x 4
## screen_name followers_count location text
## <chr> <dbl> <chr> <chr>
## 1 HarvardBiz 5581038 Boston, MA "Not all family business leaders ar~
## 2 HarvardBiz 5581038 Boston, MA "How Nasdaq evolved into a global t~
## 3 HarvardBiz 5581038 Boston, MA "Over time, your company intranet m~
## 4 HarvardHeal~ 2458514 Boston, MA "Taking blood pressure pills at bed~
## 5 RedSox 2137053 Fenway Park "If you want to get J.D. on the dan~
## 6 NHLBruins 1523387 Boston, MA "Less than an hour to go! \n\nEnter~
## 7 NHLBruins 1523387 Boston, MA "Dallas Smith.\n\nRick Smith.\n\nBi~
## 8 NHLBruins 1523387 Boston, MA "Fifty years have passed since Bobb~
## 9 MIT 1079932 Cambridge,~ "Infusing ethics into experiential ~
## 10 techreview 1075539 Cambridge,~ "The coronavirus pandemic has made ~
## 11 techreview 1075539 Cambridge,~ "Facebook and YouTube are rushing t~
## 12 techreview 1075539 Cambridge,~ "Millions of Indians are being forc~
## 13 techreview 1075539 Cambridge,~ "Covid-19 conspiracy theorists are ~
## 14 techreview 1075539 Cambridge,~ "In a simulated economy, an AI came~
## 15 techreview 1075539 Cambridge,~ "Working professionals like you, ar~
## 16 techreview 1075539 Cambridge,~ "Was that cold in February really c~
## 17 techreview 1075539 Cambridge,~ "China’s coronavirus crisis has tur~
## 18 techreview 1075539 Cambridge,~ "Give this neural network a genre, ~
## 19 techreview 1075539 Cambridge,~ "Google’s medical AI sounded impres~
## 20 techreview 1075539 Cambridge,~ "Are you feeling socially overwhelm~
Tweets_Boston %>%
top_n(10, followers_count) %>%
arrange(desc(followers_count)) %>%
ggplot() +
geom_col(aes(x = screen_name, y = followers_count)) +
labs(title = "Twitter: Popularidad de Usuarios",
x = "usuarios",
y = "cantidad")
Tweets_Boston <- lat_lng(Tweets_Boston)
Tweets_Boston_Georeferenciados <- Tweets_Boston %>%
filter(!is.na(lat), !is.na(lng))
nrow(Tweets_Boston_Georeferenciados)
## [1] 0
library(data.table)
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
##
## between, first, last
## The following object is masked from 'package:purrr':
##
## transpose
tweets_rmba <- fread("tweets_rmba.csv",
encoding = "UTF-8",
header=TRUE,
sep=",",
showProgress=TRUE,
data.table=FALSE)
tweets_rmba <- sample_frac(tweets_rmba, 0.05)
nrow(tweets_rmba)
## [1] 43187
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(leaflet)
####Debemos delimitar el área en el que vamos a trabajar, utilizando el comando bbox
bbox_Twiter <- make_bbox(lon = tweets_rmba$X, lat = tweets_rmba$Y)
bbox_Twiter
## left bottom right top
## -59.32642 -35.36662 -57.77219 -33.90090
mapa_base <- get_stamenmap(bbox_Twiter, zoom = 9)
## Source : http://tile.stamen.com/terrain/9/171/307.png
## Source : http://tile.stamen.com/terrain/9/172/307.png
## Source : http://tile.stamen.com/terrain/9/173/307.png
## Source : http://tile.stamen.com/terrain/9/171/308.png
## Source : http://tile.stamen.com/terrain/9/172/308.png
## Source : http://tile.stamen.com/terrain/9/173/308.png
## Source : http://tile.stamen.com/terrain/9/171/309.png
## Source : http://tile.stamen.com/terrain/9/172/309.png
## Source : http://tile.stamen.com/terrain/9/173/309.png
ggmap(mapa_base)
mapa_final_tweets<-get_stamenmap(bbox_Twiter,maptype="watercolor",zoom = 9)
## Source : http://tile.stamen.com/watercolor/9/171/307.jpg
## Source : http://tile.stamen.com/watercolor/9/172/307.jpg
## Source : http://tile.stamen.com/watercolor/9/173/307.jpg
## Source : http://tile.stamen.com/watercolor/9/171/308.jpg
## Source : http://tile.stamen.com/watercolor/9/172/308.jpg
## Source : http://tile.stamen.com/watercolor/9/173/308.jpg
## Source : http://tile.stamen.com/watercolor/9/171/309.jpg
## Source : http://tile.stamen.com/watercolor/9/172/309.jpg
## Source : http://tile.stamen.com/watercolor/9/173/309.jpg
options(scipen=20)
ggmap(mapa_final_tweets)+
geom_point(data=tweets_rmba,aes(x=X,y=Y,color=user_followers),alpha=.2)+
scale_color_distiller(palette="Spectral")+
labs(title="Tweets en la Region Metropolitana de Bs As",
subtitle="Tweets segun cantidad de seguidores",
caption="Fuente: Información de Twitter",
color="Seguidores")
paleta <- colorNumeric(palette = "plasma", domain = tweets_rmba$user_followers)
mapa_final_tweets_interactivo <- tweets_rmba %>%
filter(!is.na(Y), !is.na(X)) %>%
st_as_sf(coords = c("X", "Y"), crs = 4326)
paleta<-colorNumeric(palette="plasma",domain=mapa_final_tweets_interactivo$user_followers)
leaflet(mapa_final_tweets_interactivo) %>%
addTiles() %>%
addCircleMarkers(popup = ~text,
color = ~paleta(user_followers)) %>%
addLegend(title = "Followers", pal = paleta, values = ~user_followers)