Trabajo práctico N° 1 Butler- Gerlo

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.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
barrios <- st_read('https://datos.rosario.gob.ar/sites/default/files/barrios.gml')
## Reading layer `barrios' from data source `https://datos.rosario.gob.ar/sites/default/files/barrios.gml' using driver `GML'
## Simple feature collection with 50 features and 4 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -60.79693 ymin: -33.03934 xmax: -60.61278 ymax: -32.86941
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
hospitales_rosario <- st_read("https://datos.rosario.gob.ar/sites/default/files/geo_salud.gml")
## Reading layer `OGRGeoJSON' from data source `https://datos.rosario.gob.ar/sites/default/files/geo_salud.gml' using driver `GML'
## Simple feature collection with 67 features and 23 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: -60.76006 ymin: -33.02803 xmax: -60.61493 ymax: -32.88314
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
ggplot() +
    geom_sf(data = barrios) +
    geom_sf(data = hospitales_rosario, aes(color = "red", alpha = 5))

hospitales_barrios<- st_join(hospitales_rosario, barrios)
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
hospitales_barrios %>% 
    group_by(BARRIO) %>% 
    summarise(cantidad = n())
## Simple feature collection with 31 features and 2 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: -60.76006 ymin: -33.02803 xmax: -60.61493 ymax: -32.88314
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## # A tibble: 31 x 3
##    BARRIO           cantidad                                            geometry
##  * <fct>               <int>                                      <GEOMETRY [°]>
##  1 17 de agosto            1                           POINT (-60.65946 -33.011)
##  2 Alberdi                 2 MULTIPOINT ((-60.7006 -32.88314), (-60.69059 -32.8~
##  3 Alberto Olmedo          1                         POINT (-60.66491 -32.93815)
##  4 Alvear                  2 MULTIPOINT ((-60.68409 -32.97633), (-60.67627 -32.~
##  5 Antártida Argen~        1                         POINT (-60.76006 -32.94061)
##  6 Azcuenaga               1                          POINT (-60.6995 -32.94463)
##  7 Belgrano                1                         POINT (-60.71751 -32.94593)
##  8 Centro                  6 MULTIPOINT ((-60.66919 -32.95501), (-60.65146 -32.~
##  9 Cinco Esquinas          2 MULTIPOINT ((-60.69025 -32.95816), (-60.6698 -32.9~
## 10 Domingo Faustin~        2 MULTIPOINT ((-60.69602 -32.90676), (-60.68226 -32.~
## # ... with 21 more rows
ggplot() +
    geom_sf(data = barrios) +
    geom_sf(data = hospitales_barrios, aes(color = BARRIO), size=1) +
  theme(legend.position = "bottom") 

hospitales_barrios_cant <- hospitales_barrios %>%
  group_by(BARRIO) %>%
  summarise(cantidad=n())
hospitales_barrios_cant<- hospitales_barrios_cant %>%
  st_set_geometry(NULL)

head(hospitales_barrios_cant)
## # A tibble: 6 x 2
##   BARRIO              cantidad
##   <fct>                  <int>
## 1 17 de agosto               1
## 2 Alberdi                    2
## 3 Alberto Olmedo             1
## 4 Alvear                     2
## 5 Antártida Argentina        1
## 6 Azcuenaga                  1
barrios_cant <- barrios %>%
  left_join(hospitales_barrios_cant, by="BARRIO")
head(barrios_cant)
## Simple feature collection with 6 features and 5 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -60.72556 ymin: -32.99758 xmax: -60.6559 ymax: -32.89296
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##      gml_id MSLINK                        BARRIO OBSERVACIO cantidad
## 1 barrios.1     28                Victoria Walsh         10       NA
## 2 barrios.2     38                 14 de Octubre         10       NA
## 3 barrios.3     50 Docente "Hermanas Cossettini"         10       NA
## 4 barrios.4     22                 Latinoamerica         10       NA
## 5 barrios.5     47                   Bella Vista         10       NA
## 6 barrios.6     45                 Parque Casado         10       NA
##                         geometry
## 1 POLYGON ((-60.68993 -32.964...
## 2 POLYGON ((-60.66428 -32.996...
## 3 POLYGON ((-60.72556 -32.893...
## 4 POLYGON ((-60.66768 -32.968...
## 5 POLYGON ((-60.69234 -32.959...
## 6 POLYGON ((-60.66151 -32.976...
ggplot(barrios_cant) +
    geom_bar(fill=rgb(0.1,1,0.5,0.7),aes(x = BARRIO, weight = cantidad))+
    labs(title = "Hospitales por Barrio",
         fill = "Cantidad",
         caption= "Fuente: https://datos.rosario.gob.ar/",
         y="Cantidad",
         x="Barrios") +
        coord_flip()

ggplot() +
  geom_sf(data = barrios_cant, aes(fill=cantidad)) +
    labs(title = "Hospitales por Barrio",
        
         fill = "Cantidad",
         caption= "Fuente: https://datos.rosario.gob.ar/",
         y="",
         x="") +
  scale_fill_gradient(low="khaki2", high="deeppink4")

ggplot() +
  geom_sf(data = barrios_cant, aes(fill=cantidad)) +
    geom_sf_text(data=barrios, aes(label = BARRIO), size=1, colour = "black") +
    labs(title = "Hospitales por Barrio",
        
         fill = "Cantidad",
         caption= "Fuente: https://datos.rosario.gob.ar/",
         y="",
         x="") +
  scale_fill_gradient(low="khaki2", high="deeppink4")
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data