library(tidyverse)
## ── Attaching packages ───────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.0 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ──────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
airbnb <- read.csv("https://query.data.world/s/55amvafrknrgkeyeiu54yb2c6u6brc",
stringsAsFactors = FALSE)
comunas <- st_read('https://bitsandbricks.github.io/data/CABA_comunas.geojson')
## Reading layer `OGRGeoJSON' from data source `https://bitsandbricks.github.io/data/CABA_comunas.geojson' using driver `GeoJSON'
## Simple feature collection with 15 features and 4 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -58.53152 ymin: -34.70529 xmax: -58.33514 ymax: -34.52754
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
airbnb <- airbnb %>%
filter(!is.na(latitude), !is.na(longitude)) %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326)
airbnb_con_comunas <- st_join(airbnb, comunas)
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
Ahora voy a mostrar el resultado del join
ggplot() +
geom_sf(data = comunas) +
geom_sf(data = airbnb, color = "orange", alpha = .3)
`
ggplot() +
geom_sf(data = comunas) +
geom_sf(data = airbnb_con_comunas, aes(color = comunas))
conteo <-airbnb_con_comunas %>%
group_by(comunas) %>%
summarise(cantidad=n()) %>%
st_set_geometry(NULL)
## Warning: Factor `comunas` contains implicit NA, consider using
## `forcats::fct_explicit_na`
comunas <- left_join(comunas, conteo)
## Joining, by = "comunas"
ggplot()+
geom_sf(data = comunas, aes(fill = cantidad))