Primero voy a activar las librerias a utilizar. Voy a seguir trabajando con la Ciudad de Buenos Aires y los datos de One Street Maps.

library(osmdata)
## Warning: package 'osmdata' was built under R version 4.0.5
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.0.5
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.2     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.5
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.0.5
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.

Ahora voy a crear un cuadro delimitador que me va a generar cuatro coordenadas.

bbox_caba <- getbb("Ciudad Autonoma de Buenos Aires, Argentina")

Ahora voy a obtener el polĆ­gono del lĆ­mite territorial

bbox_st_poly = getbb('Ciudad Autonoma de Buenos Aires', format_out = "sf_polygon")

Ahora voy a asignar el bounding box correspondiente y especificar el dato que quiero, en este caso avenidas.

mapa_caba <- get_stamenmap(bbox = bbox_caba, zoom=13)
## Source : http://tile.stamen.com/terrain/13/2764/4934.png
## Source : http://tile.stamen.com/terrain/13/2765/4934.png
## Source : http://tile.stamen.com/terrain/13/2766/4934.png
## Source : http://tile.stamen.com/terrain/13/2767/4934.png
## Source : http://tile.stamen.com/terrain/13/2768/4934.png
## Source : http://tile.stamen.com/terrain/13/2764/4935.png
## Source : http://tile.stamen.com/terrain/13/2765/4935.png
## Source : http://tile.stamen.com/terrain/13/2766/4935.png
## Source : http://tile.stamen.com/terrain/13/2767/4935.png
## Source : http://tile.stamen.com/terrain/13/2768/4935.png
## Source : http://tile.stamen.com/terrain/13/2764/4936.png
## Source : http://tile.stamen.com/terrain/13/2765/4936.png
## Source : http://tile.stamen.com/terrain/13/2766/4936.png
## Source : http://tile.stamen.com/terrain/13/2767/4936.png
## Source : http://tile.stamen.com/terrain/13/2768/4936.png
## Source : http://tile.stamen.com/terrain/13/2764/4937.png
## Source : http://tile.stamen.com/terrain/13/2765/4937.png
## Source : http://tile.stamen.com/terrain/13/2766/4937.png
## Source : http://tile.stamen.com/terrain/13/2767/4937.png
## Source : http://tile.stamen.com/terrain/13/2768/4937.png
## Source : http://tile.stamen.com/terrain/13/2764/4938.png
## Source : http://tile.stamen.com/terrain/13/2765/4938.png
## Source : http://tile.stamen.com/terrain/13/2766/4938.png
## Source : http://tile.stamen.com/terrain/13/2767/4938.png
## Source : http://tile.stamen.com/terrain/13/2768/4938.png
## Source : http://tile.stamen.com/terrain/13/2764/4939.png
## Source : http://tile.stamen.com/terrain/13/2765/4939.png
## Source : http://tile.stamen.com/terrain/13/2766/4939.png
## Source : http://tile.stamen.com/terrain/13/2767/4939.png
## Source : http://tile.stamen.com/terrain/13/2768/4939.png

Ahora voy a identificar las avenidas

caba_ave <- opq(bbox_caba) %>% 
    add_osm_feature(key = "highway")

Ahora voy a descargar los datos.

caba_ave <- osmdata_sf(caba_ave)

Ahora voy a seleccionar solamente las calles.

caba_ave <- caba_ave$osm_lines
caba_ave <- caba_ave %>% 
  mutate(maxspeed = as.numeric(maxspeed),
         lanes = ifelse(is.na(lanes), 1, as.numeric(lanes)))

Voy a analizar el atributo de velocidad maxima en las calles de la Ciudad.

ggplot(caba_ave) +
    geom_sf(aes(color = maxspeed), alpha = 0.5) +
    scale_color_viridis_c() +
      theme_void()

Como conclusión del mapa anterior podemos distinguir que la velocidad maxima va desde 25 en algunas calles hasta 125 km/h en las autopistas. Al ser una ciudad tan grande deberiamos hacer un zoom en laguna zona como para poder visualizar mejor el atributo.

En el siguiente mapa voy a identificar cuales son las avenidas de la ciudad.

ggplot() +
    geom_sf(data = caba_ave,
             color = "gray40", alpha = .5) +
    geom_sf(data = filter(caba_ave, str_detect(name, "Avenida")), 
            color = "salmon") +
    theme_void() 

Ahora voy a pasar a analizar la cantidad de bares que hay en la ciudad de buenos aires identificando por barrio.

caba_restaurant <- read.csv("oferta_gastronomica.csv", stringsAsFactors = TRUE)
barrios_caba <- st_read("barrios_caba.geojson")
## Reading layer `barrios_caba' from data source 
##   `C:\Users\23377970594\Documents\MEU\TP 2 Clase 2\barrios_caba.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 48 features and 1 field
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -58.53152 ymin: -34.70529 xmax: -58.33515 ymax: -34.52649
## Geodetic CRS:  WGS 84

Voy a convertir el dataset con geeferencia.

caba_restaurant_geo <- caba_restaurant %>% 
    st_as_sf(coords = c("long", "lat"), crs = 4326)
datos_caba_bares <- st_join(caba_restaurant_geo, barrios_caba)

Ahora voy a agrupar por barrio y por cantidad de bares.

datos_caba_bares1  <- datos_caba_bares %>%
  group_by(BARRIO) %>%
  summarise(cantidad=n())
datos_caba_bares2 <-st_join(caba_restaurant_geo, barrios_caba)

En el siguiente grafico podemos ver la cantidad de bares por barrio.

ggplot(datos_caba_bares1)+
  geom_bar(aes (x=BARRIO, weight=cantidad)) +
  labs(title = "Bares en CABA")

Voy a sacarle la columna de georeferencia apra poder unir los datasets.

datos_caba_bares3 <- datos_caba_bares1 %>%
  st_set_geometry(NULL)
datos_bares_cabafinal <- left_join(barrios_caba, datos_caba_bares3, by= "BARRIO")

Ahora voy a graficar la cantidad de bares por barrio.

ggplot()+
  geom_sf(data = datos_bares_cabafinal, aes(fill=cantidad)) +
  labs(title = "Cantidad de bares en CABA por barrio")

A partir del mapa anterior podemos ver la cantidad de bares en la Ciudad de Buenos Aires, podemos concluir que la mayor cantidades de bares se encuentran en Palermo con mas de 400 bares, siguiendole el barrio de Recoleta. Los barrios de la periferia son los que menor cantidad de bares tienen, menos de 100 por barrio.