TP1_" CALCULANDO Y MAPEANDO AGREGADOS POR ÁREA + ADQUIRIENDO OPEN DATA URBANA"

PasoS I y II

Cargamos las librerias que necesitamos para trabajar.

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

Seleccionamos una ciudad Boston con un dataset con registros geo-referenciados con sus coordenadas: Bibliotecas Públicas.

Leemos el dataset que llamaeremos Bibliotecas_Publicas_OK; utilizando el comando st_read que lo lee directamente desde la web.

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

Filtramos los datos que nos interesan de nuestro dataset y que quede más ordenado:

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)

Ahora leemos el dataset de Barrios de Boston, utilizando el comando st_read ya que es un elemento geográfico (un shapefile).

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"

Vamos a explorar el dataset Barrios_Boston, utilizando el comando plot, para tener una rápida observación del mismo.

plot(Barrios_Boston_OK)

Observamos las 7 variables de nuestro dataset de forma correcta, algunas mas relevantes que otras.

Ahora cruzamos nuestros datos de Barrios y Bibliotecas Públicas en un mapa a ver que observamos:

ggplot() +
  geom_sf(data = Barrios_Boston_OK) +
  geom_sf(data = Bibliotecas_Publicas_Ordenadas,
          color = "red")

#### Observamos bibliotecas asignadas a los distintos barrios.

Verificamos los sistemas de coordenadas de ambos datasets, y si no coinciden los transformamos a EPSG:4326.

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)

Verificamos si el datset de Barrios se transformo correctamente al sistema de coordenadas de Proyeccion Mercator (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...

Paso III

Ahora mediante un join espacial, utilizando el comando st_join(). A cada biblioteca le asignaremos el barrio que le corresponda.

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"

Con este nuevo dataset, estamos en condiciones de saber, cuantas Bibliotecas Públias hay por Barrio = Neighborho. Asimismo, podremos graficar mapas de valores máximos, mínimos, promedios, entre otros, de la cantidad de Bibliotecas que haya por Barrio.

Paso IV

Vamos a graficar Un gráfico de puntos para visualizar la cantidad de bilioteas por barrio. Primero debemos agrupar los bibliotecas por barrio = Neighborho.

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)

Ahora realizamos el grafico de puntos:

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")

Mapeamos las bibliotecas por barrio, distinguiendo por color:

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")

Observamos que en los barrios prevalecen 1 Biblioteca pública por Barrio aunque en 4 Barrios se destacan 2 Bibliotecas. Es llamativo en el Barrio ID: 6 la concentración de 5 Bibliotecas públicas, habrá que estudiar a que se debe esto.

Vamos a pintar cada barrio = Neighborho con la cantidad máxima de Bibliotecas Públicas, utilizando un gráfico de coropletas.

Para ello utilizamos al dataset Total_Bibliotecas_Publicas_Ordenadas_con_Barrios y le quitamos la geometría. Luego lo unimos al dataset Barrios_Boston:

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

Modificamos el nombre de la columa Total = Total Bibliotecas Publicas

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

Mediante el comando left_join() unificamos el datasat Total_Bibliotecas_Publicas_Ordenadas_con_Barrios con Barrios_Boston:

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"

Finalmente mapeamos las bibliotecas por barrio :

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

Se observa como cambia el gradiente por barrio según la cantidad de bibliotecas que hay en cada uno de ellos. Los barrios en color gris refiere que no hay presencia de biblioteas públicas.

TP2_“EXPLORANDO Y MAPEANDO INFORMACIÓN GERREFERENCIADA DE OPENSTREETMAP”

Cargamos las librerias que necesitamos para trabajar.

En primer lugar debemos instalar el paquete osmdata que nos permitira acceder a los datos de OpenStreetMap y el paquete leaflet que nos posiilitará generar napas intercativos de forma rápida. Asimismo, intalamos el paquete ggsflabel que nos permitirá agregar etiquetas, sin que estas se solapen en un mapa.Luego cargamos sus librerias correspondientes.

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

Paso I

Debemos descargar de OpenStreetMap la grilla de las calles de la Ciudad de Boston. Para ello primero debemos delimiar la ciudad.

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")

Verifiquemos que los limites que obtuvimos coincidan con nuestra area de interes, utilizando el comando “leaflet”.

leaflet(bbox_poly_Boston) %>% 
    addTiles() %>% 
    addPolygons()

#### Efectivamente se corresponde con la Ciudad de Boston que veníamos trabajando.

Ahora debemos decargar la información, utilizando el comando osmdata_sf(). Vamos a descargar las Calles = Highway y vamos a mapearlas segun sus atributos

Boston_Calles <- opq(bbox_Boston) %>% 
    add_osm_feature(key = "highway") %>% 
    osmdata_sf()

Recortamos las calles que exceden nuestra ciudad, utilizando el comando st_intersection

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")  

Paso II

####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

Paso II-a

Mapeamos para ver la distibución general de Bibliotecas en la Ciudad

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.

Paso II-b

Ahora agruparemos las biliotecas por barrio y utilizando un mapa de coropletas veremos como se distibuyen la mayor cantidad de biliotecas en la ciudad.

####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"

Ahora agrupamos por barrio:

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

Utilizando el comando left_join() unificamos el datasat Total_Bibliotecas_OSM_en_Barrios con Barrios_Boston_OK

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...

Finalmente mapeamos

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

Para obtener una mejor observación de la distribución de las Bibliotecas por Barrio realizaremos un gráfico de puntos, de este modo tendremos la información completa para comprar nuestra información actual con la Tarea 1

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).

Conclusion

Se advierte que las Biliotecas de OSM alcanzan 23 Barrios de la ciudad, mientras que Bibliotecas Públicas estan presentes en 18 Barrios. Asimismo, la cantidad de entidades que pueden observarse en cada uno de ellos es muy distinto. En el dataset de Biliotecas de OSM los barrios tienen un mínimo de 10 establecimientos, mientras que el dataset de Bibliotecas Públicas tiene 1 establecimiento.

Además, en el primer dataset mencionado, se distingue un rango entre 10 y 75 Bibliotecas, primando el Barrio ID 6 con mas de 200 entidades. Por otro lado, en los barrios donde se encuentran presentes las Bibliotecas Públicas, éstas oscilan entre 1 y 2 entidades, pero se asemeja con la concentración de bibliotecas en el Barrio ID 6. Podemos deducir que esta es el área central de la ciudad, donde se concentran actividades, servicios e infraestructura.

Los Barrios ID 11, 12, 16 y 25 contaban con 2 Bibliotecas Públicas y considerando las Bibliotecas de OSM llegan a un alcance aproximado de 25 establecimientos.

Al considerar las Bibliotecas OSM se observan nuevos barrios que no contaban con instituciones, alcanzando altos números como los Barrios ID 2, 24, 28 y 34 que tienen un promedio aproximado entre 50 y 75 bibliotecas. Podría considerarse a dichos distritos como núcelos urbanos con alta densidad, necesarios de contar con el mencionado servicio.

Por otro lado realizaremos un mapa de coropletas, para contemplar las densidad de Bibliotecas por Barrio:

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

Verificamos al igual que en la Tarea 1, que la distribución de Bibliotecas no es coincidente con la superficie de los Barrios. Los Barrios con menor superfice, suelen tener la mayor cantidad de densidad de Bibliotecas. A diferencia que en el dataset de Bibliotecas Públicas, donde la mayor densidad se disntiguía en el Barrio ID 14, aquí la mayor densidad se da en el Barrio ID 13. La similitud entre ambos dataset Bibliotecas Públicas y Bibliotecas OMS se da en que los barrios más pequeños concentran la mayor densidad de bibliotecas.

TP3_“CAPTURANDO Y EXPLORANDO DATOS DE TWITTER”

Paso I

En primer lugar debemos descargar los datos de Twitter generados alrededor de la ciudad de Boston. Para en primer lugar debemos instalar el paquete rtweet para poder tener acceso a Twitter y activarlo.

library(rtweet)
## 
## Attaching package: 'rtweet'
## The following object is masked from 'package:purrr':
## 
##     flatten

Luego cargamos la información, utilizando la base de datos descargada de Twitter utilizando las coordenadas de Boston y un Radio = 7 millas. El dataset fue descargado por un usuario de Twitter ya que no obtuvimos la aprobación de solicitud de acceso a API.

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>

Exploramos el dataset

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>

Observamos que el dataset Tweets_Boston contiene 17.696 Filas, que refieren a la cantidad de Tweets encontrados en Boston y 90 Columnas, haciendo referencia a los detalles de cada uno de los Tweets.

Paso II A

Al haber tanta cantidad de observaciones, es interesante analizar cuales son los mensajes con mayor repercusión y verificar si existen anunciones que preponderan en su temática.

Para ello debemos identificar los tweets que tuvieron mas retweets.

Primero realizamos un histograma filtrando los tweets originales y observamos su distribución.

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`.

Se observa una gran cantidad de tweets originales respecto al total de observaciones. Asimismo, en el gráfico incorporamos el comando scipen para eliminar la posible notación científica.

Para continuar es importante identificar en esa gran cantidad de observaciones de tweets originales cuales son los que tienen mayor repercusión. Utilizamos el comando top_n y aplicamos 6 para distinguir a los 5 primeros de la lista. Asimismo, seleccionamos determinadas varaiables, incluyendo text para ver a que temas refieren los mensajes.

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, ~

Observamos que los mensajes de jujuboston, ValaAfshar, jasonfurman, ValaAfshar, TeamAlbanians y FieldYates son los que mayor repercusión tienen. Los mensajes de los mismos, refieren a: jujuboston: el agradecimiento frente a una oportunidad que le ha sido asignada. ValaAfshar: reconociendo la buena actitud de otros, podemos suponer que reconoce la buena actitud de los demás frente a la situación actual del Covid-19. jasonfurman: referencia a la estratégia Sueca para el desarrollo de su economía, también podemos suponer que refiere a la situación vigente Covid-19. ValaAfshar (también en 2do lugar): frase inspiradora haciendo referencia al amor de padres e hijos. TeamAlbanians: referencia a la igualdad entre blancos y negros. *FieldYates:

Se puede analizar que no hay correlación entre los mensajes, aunque estimamos que dos mensajes podrían estar relacionado a la situación vigente del Covid-19. También, podemos suponer que ValaAfshar tiene un perfil emocional ya que sus dos mensajes de algún modo apelan a los sentimientos. A su vez, se examina que los usuarios jasonfurman y TeamAlbanians poseen un cantidad de seguidores muy inferior al resto de los usurarios jujuboston, ValaAfshar y FieldYates y sus tweets alcanzaron la mayor repercusión.

Paso II B

Graficaremos y analizaremos en que momento del día se realizan la mayor cantidad de tweets, utilizando el comando ts_plot, observando la frecuencia de tweets por minutos.

ts_plot(Tweets_Boston, "minutes")

Claramente se distingue que de nuestro dataset la hora pico es a las 16:00, habiendo dos frecuencias menores a las 16:30 y 17:00. El resto de los datos se distribuyen de forma semejante.

Paso II C

Analizaremos la distribución de la popularidad de los usuarios y los 5 usuarios con más seguidores.

En primer lugar realizamos un graficamos un histograma.

ggplot(Tweets_Boston) +
  geom_histogram(aes(x = followers_count))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Observamos que los datos se distribuyen entre usuarios populares y usuarios sin popularidad. Para distinguir dentro de los usuarios populares quienes son los 5 más populares, utilizaremos el comando top_n, incorporando el número 5. Seleccionamos ciertos datos que pueden resultar de interés.

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~

Como la cuenta en la primera, segunda y tercera posición de popularidad es la misma, perteneciente a HarvardBiz, incluso la cuarta cuenta, HarvardHealth, incluiremos 5 observaciones más en nuestro análisis para ver cuales son los usuarios que pueden acceder al Top 10 de popularidad

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~

Al incorporar cinco nuevas observaciones 3 nuevas cuentas se incorporan. Son 3 y no 5, ya que NHLBruins toma tres posiciones del listado. Es llamativo el caso de techreview que toma la posición 10, pero a las vez se incorporan 10 observaciones más de usuarios con el mismo nombre, ocupando las posiciones de las 11 a la 20.

Ahora graficaremos el Top 10

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")

Se observan los 6 usuarios con mayor popularidad, con 20 observaciones graficadas.

Paso II-D

Ahora crearemos mapas interactivos en los cuales se podrán ver las posiciones de los tweets, especificando la cantidad de seguidores que posee el usuario que tuitea.

Para ello, en primer lugar debemos aisalar los twits que poseen coordenadas geográficas (lat y long)

Tweets_Boston <- lat_lng(Tweets_Boston)
Tweets_Boston_Georeferenciados <- Tweets_Boston %>% 
  filter(!is.na(lat), !is.na(lng))
nrow(Tweets_Boston_Georeferenciados)
## [1] 0

PROBLEMA: no hay datos georeferenciados, con lo cual vamos a recurrir a la Ciudad de Buenos Aires , seleccionando solo una parte ya que contiene muchos datos.

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)

Ahora seleccionamos solo una muestra del 5% de los datos e la Region Metropolitana de Buenos Aires.

tweets_rmba <- sample_frac(tweets_rmba, 0.05)
nrow(tweets_rmba)
## [1] 43187

Para graficar primero debemos instalar dos paquetes ggmap y leaflet, luego activar sus respectivas librerías.

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

Con estas coordenadas descargaremos nuestro mapa de Stamen Design, delimitado por la bounding box del área que nos interesa.

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)

Graficamos incorporando capas de datos que muestren la posición de los tweets y su cantidad de seguidores

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")

MAPA INTERACTIVO

Trabajaremos con leaflet, incorporando capas de datos que muestren la posición de los twitts, con su texto y la canidad de seguidores y seguidos de cada usuario.Lo lograremos selecionado una paleta de color.

Antes transformamos el mapa en proyeccion mercator.

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)