library(rtweet)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.0 v purrr 0.3.3
## v tibble 2.1.3 v dplyr 0.8.4
## 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 purrr::flatten() masks rtweet::flatten()
## x dplyr::lag() masks stats::lag()
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
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)
tweets_deuda <- search_tweets(q = "DeudaSostenible OR Deuda OR FuturoSostenible OR acreedores OR reestructuración",
geocode = "-34.603722,-58.381592,20mi",
include_rts = FALSE,
n = 100000,
retryonratelimit = TRUE)
count(tweets_deuda)
## # A tibble: 1 x 1
## n
## <int>
## 1 4785
tweets_deuda <- tweets_deuda %>%
mutate(fecha = ymd_hms(created_at)) %>%
rename( procedencia = source )
head(tweets_deuda)
## # A tibble: 6 x 91
## user_id status_id created_at screen_name text procedencia
## <chr> <chr> <dttm> <chr> <chr> <chr>
## 1 109203~ 12617565~ 2020-05-16 20:33:07 Vorty6 "Te ~ Twitter We~
## 2 347674~ 12617564~ 2020-05-16 20:32:38 Sir_Juance~ "@di~ Twitter We~
## 3 347674~ 12617559~ 2020-05-16 20:30:42 Sir_Juance~ "@di~ Twitter We~
## 4 347674~ 12617554~ 2020-05-16 20:28:36 Sir_Juance~ "@di~ Twitter We~
## 5 347674~ 12611405~ 2020-05-15 03:45:03 Sir_Juance~ "@di~ Twitter We~
## 6 347674~ 12617213~ 2020-05-16 18:12:54 Sir_Juance~ "@di~ Twitter We~
## # ... with 85 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 <int>, retweet_count <int>, quote_count <int>,
## # reply_count <int>, hashtags <list>, symbols <list>, urls_url <list>,
## # urls_t.co <list>, urls_expanded_url <list>, media_url <list>,
## # media_t.co <list>, media_expanded_url <list>, media_type <list>,
## # ext_media_url <list>, ext_media_t.co <list>, ext_media_expanded_url <list>,
## # ext_media_type <chr>, mentions_user_id <list>, mentions_screen_name <list>,
## # lang <chr>, quoted_status_id <chr>, quoted_text <chr>,
## # quoted_created_at <dttm>, quoted_source <chr>, quoted_favorite_count <int>,
## # quoted_retweet_count <int>, quoted_user_id <chr>, quoted_screen_name <chr>,
## # quoted_name <chr>, quoted_followers_count <int>,
## # quoted_friends_count <int>, quoted_statuses_count <int>,
## # quoted_location <chr>, quoted_description <chr>, quoted_verified <lgl>,
## # retweet_status_id <chr>, retweet_text <chr>, retweet_created_at <dttm>,
## # retweet_source <chr>, retweet_favorite_count <int>,
## # retweet_retweet_count <int>, retweet_user_id <chr>,
## # retweet_screen_name <chr>, retweet_name <chr>,
## # retweet_followers_count <int>, retweet_friends_count <int>,
## # retweet_statuses_count <int>, retweet_location <chr>,
## # retweet_description <chr>, retweet_verified <lgl>, place_url <chr>,
## # place_name <chr>, place_full_name <chr>, place_type <chr>, country <chr>,
## # country_code <chr>, geo_coords <list>, coords_coords <list>,
## # bbox_coords <list>, status_url <chr>, name <chr>, location <chr>,
## # description <chr>, url <chr>, protected <lgl>, followers_count <int>,
## # friends_count <int>, listed_count <int>, statuses_count <int>,
## # favourites_count <int>, 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>, fecha <dttm>
procedencia_frecuente <- tweets_deuda %>%
count(procedencia) %>%
top_n(5) %>%
pull(procedencia)
## Selecting by n
head (procedencia_frecuente)
## [1] "TweetDeck" "Twitter for Android" "Twitter for iPhone"
## [4] "Twitter Web App" "Twitter Web Client"
tweets_deuda %>%
filter (procedencia %in% procedencia_frecuente) %>%
ggplot() +
geom_bar(aes(x = wday (fecha, label = TRUE, abbr=FALSE), fill = procedencia), position = "dodge")+
labs(title = "Distribución por día de tweets realizados según su procedencia",
x = "Día de la semana", y = "Cantidad",
fill = "Procedencias más frecuentes",
subtitle = "AMBA",
caption = "Fuente: Twitter") +
scale_fill_viridis_d()
conteo <- tweets_deuda %>%
filter( procedencia %in% procedencia_frecuente) %>%
count(procedencia, diasemana = wday(fecha, label = TRUE, abbr=FALSE)) %>%
group_by(procedencia) %>%
mutate(pct = n / sum(n) * 100)
ggplot(conteo) +
geom_line(aes(x = diasemana, y = pct, group = procedencia, color = procedencia))+
expand_limits(y = 0)+
scale_color_viridis_d()+
theme_dark ()+
labs(title = "Distribución por día de tweets realizados según su procedencia comparando porcentajes",
x = "Día de la semana", y = "pct",
fill = "Procedencias más frecuentes",
subtitle = "AMBA",
caption = "Fuente: Twitter")
por_hora <- tweets_deuda %>%
filter(procedencia %in% procedencia_frecuente) %>%
count(procedencia, hora_base = hour(ymd_hms(fecha))) %>%
group_by(procedencia) %>%
mutate(pct = n / sum(n) *100)
ggplot(por_hora) +
geom_line(aes(x = hora_base, y = pct, group = procedencia, color = procedencia)) +
labs(title = "Distribución horaria de tweets",
x = "hora", y = "%", color = "Procedencia más frecuente", subtitle = "AMBA",
caption = "Fuente: Twitter") +
expand_limits(y = 0) +
scale_x_continuous(breaks = 0:23)
tweets_deuda_geo <- lat_lng(tweets_deuda) %>%
select(-geo_coords, -coords_coords, -bbox_coords) %>%
filter(!is.na(lat), !is.na(lng))
nrow(tweets_deuda_geo)
## [1] 519
tweets_deuda_geo <- tweets_deuda_geo %>%
filter(lat <0, lng <0)
bbox <- make_bbox(lon = tweets_deuda_geo$lng, lat = tweets_deuda_geo$lat)
bbox
## left bottom right top
## -58.83245 -34.85154 -58.17054 -34.42451
mapa_BA <- get_stamenmap(bbox = bbox, maptype = "terrain-lines", zoom = 11)
## Source : http://tile.stamen.com/terrain-lines/11/689/1232.png
## Source : http://tile.stamen.com/terrain-lines/11/690/1232.png
## Source : http://tile.stamen.com/terrain-lines/11/691/1232.png
## Source : http://tile.stamen.com/terrain-lines/11/692/1232.png
## Source : http://tile.stamen.com/terrain-lines/11/693/1232.png
## Source : http://tile.stamen.com/terrain-lines/11/689/1233.png
## Source : http://tile.stamen.com/terrain-lines/11/690/1233.png
## Source : http://tile.stamen.com/terrain-lines/11/691/1233.png
## Source : http://tile.stamen.com/terrain-lines/11/692/1233.png
## Source : http://tile.stamen.com/terrain-lines/11/693/1233.png
## Source : http://tile.stamen.com/terrain-lines/11/689/1234.png
## Source : http://tile.stamen.com/terrain-lines/11/690/1234.png
## Source : http://tile.stamen.com/terrain-lines/11/691/1234.png
## Source : http://tile.stamen.com/terrain-lines/11/692/1234.png
## Source : http://tile.stamen.com/terrain-lines/11/693/1234.png
## Source : http://tile.stamen.com/terrain-lines/11/689/1235.png
## Source : http://tile.stamen.com/terrain-lines/11/690/1235.png
## Source : http://tile.stamen.com/terrain-lines/11/691/1235.png
## Source : http://tile.stamen.com/terrain-lines/11/692/1235.png
## Source : http://tile.stamen.com/terrain-lines/11/693/1235.png
ggmap(mapa_BA) +
geom_bin2d(data = tweets_deuda_geo, aes(x = lng, y = lat), bins=50) +
scale_color_viridis_c()+
theme_classic ()+
labs(title = "Distribución de tweets a partir de un Mapa de Densidad",
x = "", y = "",
fill = "Cantidad de Tweets",
subtitle = "AMBA",
caption = "Fuente: Twitter")
ggmap(mapa_BA) +
geom_point(data = filter(tweets_deuda_geo, procedencia %in% procedencia_frecuente),
aes(x = lng, y = lat, color = procedencia),
size = 5) +
scale_color_brewer(palette = "Set1") +
facet_wrap(~procedencia)+
theme_minimal()+
labs(title = "Distribución de tweets a partir de un mapa facetado por Procedencia",
x = "", y = "",
color = "Procedencia",
subtitle = "AMBA",
caption = "Fuente: Twitter")
tweets_deuda_geo <- tweets_deuda_geo %>%
mutate(dia_semana = wday(fecha, label = TRUE, abbr=FALSE))
ggmap(mapa_BA) +
geom_point(data = filter(tweets_deuda_geo,
procedencia %in% c("Twitter for Android", "Twitter for iPhone")),
aes(x = lng, y = lat, color = procedencia), size = 4) +
facet_wrap(~dia_semana)+
theme_minimal()+
labs(title = "Distribución de tweets a partir de un mapa facetado por Procedencia",
x = "", y = "",
color = "Procedencia",
subtitle = "AMBA",
caption = "Fuente: Twitter")
operativos <- read.csv("D:/Documents/01-Ditella/Ciencia de datos2/Clase 4/operativos-2018.csv") %>%
mutate(fecha = ymd(fecha))
head(operativos)
## long lat fecha suceso
## 1 -58.51115 -34.65988 2018-01-01 CABLES EXPUESTOS / CAIDOS
## 2 -58.43076 -34.62255 2018-01-01 BARRIDO / DERRAME
## 3 -58.43859 -34.63597 2018-01-01 CABLES EXPUESTOS / CAIDOS
## 4 -58.46343 -34.63317 2018-01-01 LLENADO DE TANQUE
## 5 -58.48211 -34.63318 2018-01-01 LLENADO DE TANQUE
## 6 -58.45266 -34.61078 2018-01-01 LLENADO DE TANQUE
## calle_nombre calle_altura barrio comuna
## 1 CARHUE 2080 MATADEROS COMUNA 9
## 2 ALBERDI JUAN BAUTISTA AV Y DOBLAS 0 CABALLITO COMUNA 6
## 3 SANTANDER Y MITRE EMILIO 0 PARQUE CHACABUCO COMUNA 7
## 4 BONIFACIO JOSE 2566 FLORES COMUNA 7
## 5 CHIVILCOY 74 FLORESTA COMUNA 10
## 6 GAONA 1723 CABALLITO COMUNA 6
## observacion movil a_cargo turno emergencia prevencion
## 1 157 SALINAS F/S si no
## 2 855 PASCUALENA F/S si no
## 3 140 MARTINEZ F/S si no
## 4 371 MEZA F/S si no
## 5 371 MEZA F/S si no
## 6 371 MEZA F/S si no
## amenaza mes codigo_postal
## 1 OTROS ENERO 1440
## 2 14) INTOXICACIONES Y CONTAMINACIONES ENERO NA
## 3 OTROS ENERO NA
## 4 13) INTERRUPCION DE SERVICIOS BASICOS ENERO 1406
## 5 13) INTERRUPCION DE SERVICIOS BASICOS ENERO 1407
## 6 13) INTERRUPCION DE SERVICIOS BASICOS ENERO 1416
## codigo_postal_argentino
## 1 C1440EQN
## 2
## 3
## 4 C1406GYD
## 5 C1407ADB
## 6 C1416DRF
operativos_frecuente <- operativos %>%
count(suceso) %>%
top_n(5) %>%
pull(suceso)
## Selecting by n
head (operativos_frecuente)
## [1] ARBOLES / RAMAS
## [2] CABLES EXPUESTOS / CAIDOS
## [3] ESCAPE DE GAS
## [4] PELIGRO DE CAIDA COLUMNA / POSTE / CARTEL / LUMINARIA
## [5] RIESGO ELECTRICO
## 46 Levels: ACCIDENTE AGUAS SERVIDAS AMENAZA DE BOMBA ... VISUALIZACION DE HUMO
operativos %>%
filter(suceso %in% operativos_frecuente) %>%
ggplot() +
geom_bar(aes(x = month (fecha, label = TRUE, abbr=FALSE), fill = suceso))+
coord_flip()+
labs(title = "Principales sucesos - Defensa Civil",
x = "Mes", y = "Cantidad",
fill = "Sucesos más frecuentes",
subtitle = "Ciudad Autónoma de Buenos Aires, 2018", caption = "Fuente: portal de datos abiertos de la Ciudad - http://data.buenosaires.gob.ar") +
scale_fill_viridis_d()
conteo <- operativos %>%
filter( suceso %in% operativos_frecuente) %>%
count(suceso, diasemana = wday(fecha, label = TRUE, abbr = FALSE))
ggplot(conteo) +
geom_line(aes(x = diasemana, y = n, group = suceso, color = suceso))+
theme_dark ()+
labs(title = "Principales sucesos - Defensa Civil",
x = "Día de la Semana", y = "Cantidad",
fill = "Sucesos más frecuentes",
subtitle = "Ciudad Autónoma de Buenos Aires, 2018", caption = "Fuente: portal de datos abiertos de la Ciudad - http://data.buenosaires.gob.ar")
operativos_geo <- operativos %>%
filter(lat <0, long <0)
head(operativos_geo)
## long lat fecha suceso
## 1 -58.51115 -34.65988 2018-01-01 CABLES EXPUESTOS / CAIDOS
## 2 -58.43076 -34.62255 2018-01-01 BARRIDO / DERRAME
## 3 -58.43859 -34.63597 2018-01-01 CABLES EXPUESTOS / CAIDOS
## 4 -58.46343 -34.63317 2018-01-01 LLENADO DE TANQUE
## 5 -58.48211 -34.63318 2018-01-01 LLENADO DE TANQUE
## 6 -58.45266 -34.61078 2018-01-01 LLENADO DE TANQUE
## calle_nombre calle_altura barrio comuna
## 1 CARHUE 2080 MATADEROS COMUNA 9
## 2 ALBERDI JUAN BAUTISTA AV Y DOBLAS 0 CABALLITO COMUNA 6
## 3 SANTANDER Y MITRE EMILIO 0 PARQUE CHACABUCO COMUNA 7
## 4 BONIFACIO JOSE 2566 FLORES COMUNA 7
## 5 CHIVILCOY 74 FLORESTA COMUNA 10
## 6 GAONA 1723 CABALLITO COMUNA 6
## observacion movil a_cargo turno emergencia prevencion
## 1 157 SALINAS F/S si no
## 2 855 PASCUALENA F/S si no
## 3 140 MARTINEZ F/S si no
## 4 371 MEZA F/S si no
## 5 371 MEZA F/S si no
## 6 371 MEZA F/S si no
## amenaza mes codigo_postal
## 1 OTROS ENERO 1440
## 2 14) INTOXICACIONES Y CONTAMINACIONES ENERO NA
## 3 OTROS ENERO NA
## 4 13) INTERRUPCION DE SERVICIOS BASICOS ENERO 1406
## 5 13) INTERRUPCION DE SERVICIOS BASICOS ENERO 1407
## 6 13) INTERRUPCION DE SERVICIOS BASICOS ENERO 1416
## codigo_postal_argentino
## 1 C1440EQN
## 2
## 3
## 4 C1406GYD
## 5 C1407ADB
## 6 C1416DRF
encuadre <- make_bbox(lon = operativos_geo$long, lat = operativos_geo$lat)
mapa_BA2 <- get_stamenmap(encuadre, maptype = "terrain-lines", zoom = 12)
## Source : http://tile.stamen.com/terrain-lines/12/1381/2466.png
## Source : http://tile.stamen.com/terrain-lines/12/1382/2466.png
## Source : http://tile.stamen.com/terrain-lines/12/1383/2466.png
## Source : http://tile.stamen.com/terrain-lines/12/1384/2466.png
## Source : http://tile.stamen.com/terrain-lines/12/1381/2467.png
## Source : http://tile.stamen.com/terrain-lines/12/1382/2467.png
## Source : http://tile.stamen.com/terrain-lines/12/1383/2467.png
## Source : http://tile.stamen.com/terrain-lines/12/1384/2467.png
## Source : http://tile.stamen.com/terrain-lines/12/1381/2468.png
## Source : http://tile.stamen.com/terrain-lines/12/1382/2468.png
## Source : http://tile.stamen.com/terrain-lines/12/1383/2468.png
## Source : http://tile.stamen.com/terrain-lines/12/1384/2468.png
## Source : http://tile.stamen.com/terrain-lines/12/1381/2469.png
## Source : http://tile.stamen.com/terrain-lines/12/1382/2469.png
## Source : http://tile.stamen.com/terrain-lines/12/1383/2469.png
## Source : http://tile.stamen.com/terrain-lines/12/1384/2469.png
ggmap(mapa_BA2) +
geom_density2d(data = operativos_geo, aes(x = long, y = lat, color = stat(level)),size = 1.5) +
scale_color_viridis_c()+
theme_classic ()+
labs(title = "Distribución de operativos de Defensa Civil",
x = "", y = "",
color = "Cantidad de operativos",
subtitle = "Ciudad Autónoma de Buenos Aires, 2018", caption = "Fuente: portal de datos abiertos de la Ciudad - http://data.buenosaires.gob.ar")
ggmap(mapa_BA2) +
geom_point(data = filter(operativos_geo, suceso %in% operativos_frecuente),
aes(x = long, y = lat, color = suceso),
size = 0.5) +
scale_color_brewer(palette = "Set1") +
facet_wrap(~suceso)+
theme_replace()+
labs(title = "Distribución de operativos de Defensa Civil",
x = "", y = "",
color = "Cantidad de operativos",
subtitle = "Ciudad Autónoma de Buenos Aires, 2018", caption = "Fuente: portal de datos abiertos de la Ciudad - http://data.buenosaires.gob.ar")+
theme(strip.text.x = element_text(size = 8, colour = "black"))
operativos_geo <- operativos_geo %>%
mutate(dia_semana = wday(fecha, label = TRUE, abbr = FALSE))
ggmap(mapa_BA2) +
geom_point(data = filter(operativos_geo,
suceso %in% c("ESCAPE DE GAS", "ARBOLES / RAMAS")),
aes(x = long, y = lat, color = suceso), alpha = 1, size = 1) +
facet_wrap(~dia_semana)+
theme_replace()+
labs(title = "Distribución de operativos de Defensa Civil",
x = "", y = "",
color = "Cantidad de operativos",
subtitle = "Ciudad Autónoma de Buenos Aires, 2018", caption = "Fuente: portal de datos abiertos de la Ciudad - http://data.buenosaires.gob.ar")+
theme(strip.text.x = element_text(size = 15, colour = "black"))