sábado, 7 de diciembre de 2019

Streetmaps: Crea un mapa de tu ciudad con ggplot2

Streetmaps with ggplot2

Map Barquisimeto-Cabudare, Venezuela.

Here i show you how following step by step the tutorial instruction give in http://ggplot2tutor.com/streetmaps/streetmaps/, we can will generate code in R (ggplot2) which output are streets maps of our city.

Aquí te muestro cómo siguiendo paso a paso las instrucciones del tutorialhttp://ggplot2tutor.com/streetmaps/streetmaps/, podemos generar código en R (ggplot2) cuyo resultado son mapas de calles de nuestra ciudad.

This work was made for the City Barquisimeto-Cabudare, the city where I currently live in the Venezuela country.

we needs to instal omsdata and ggplot2 packages, ggplot2 is in tidyverse, so is enough instal it.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.3
## -- Attaching packages --------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.0       v purrr   0.3.2  
## v tibble  2.1.1       v dplyr   0.8.0.1
## v tidyr   0.8.3       v stringr 1.3.1  
## v readr   1.1.1       v forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.5.3
## Warning: package 'tibble' was built under R version 3.5.3
## Warning: package 'tidyr' was built under R version 3.5.3
## Warning: package 'purrr' was built under R version 3.5.3
## Warning: package 'dplyr' was built under R version 3.5.3
## Warning: package 'forcats' was built under R version 3.5.3
## -- Conflicts ------------------------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(tinytex)
## Warning: package 'tinytex' was built under R version 3.5.3

We will use the package osmdata to extract the streets from the Openstreetmap database. Openstreetmap is a freely available database of all locations in the world under an open license. Next we will use the funtion getbb() to determiner the coordenade longitude and latitude.

You can to adjust the longitude and latitude of your preferred city, just change the location in the function getbb().

library(osmdata)
## Warning: package 'osmdata' was built under R version 3.5.3
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
getbb("Cabudare im Venezuela")
##          min       max
## x -69.415231 -69.09523
## y   9.867945  10.18794
streets <- getbb("cabudare Venezuela")%>%
  opq()%>%
  add_osm_feature(key = "highway", 
                  value = c("motorway", "primary", 
                            "secondary", "tertiary")) %>%
  osmdata_sf()
streets
## Object of class 'osmdata' with:
##                  $bbox : 9.8679447,-69.4152305,10.1879447,-69.0952305
##         $overpass_call : The call submitted to the overpass API
##                  $meta : metadata including timestamp and version numbers
##            $osm_points : 'sf' Simple Features Collection with 8873 points
##             $osm_lines : 'sf' Simple Features Collection with 465 linestrings
##          $osm_polygons : 'sf' Simple Features Collection with 3 polygons
##        $osm_multilines : NULL
##     $osm_multipolygons : NULL
small_streets <- getbb("Cabudare im Venezuela")%>%
  opq()%>%
  add_osm_feature(key = "highway", 
                  value = c("residential", "living_street",
                            "unclassified",
                            "service", "footway")) %>%
  osmdata_sf()

river <- getbb("Cabudare im Venezuela")%>%
  opq()%>%
  add_osm_feature(key = "waterway", value = "river") %>%
  osmdata_sf()

Plot 1

We´ll star with a plot basic, which to show you the principals streets our city

ggplot() +
  geom_sf(data = streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .4,
          alpha = .8) +
  coord_sf(xlim = c(-69.50, -69.05), 
           ylim = c(9.99, 10.20),
           expand = FALSE)

Plot 2

This plot conteins add features as details with a bit precision. there are more avalaibles features can be useful,To get all tags of a feature via osmdata, you can enter the following function available_tags() or if you want more details visit http://ggplot2tutor.com/streetmaps/streetmaps/.

ggplot() +
  geom_sf(data = streets$osm_lines,
          inherit.aes = FALSE,
          color = "steelblue",
          size = .4,
          alpha = .8) +
  geom_sf(data = small_streets$osm_lines,
          inherit.aes = FALSE,
          color = "black",
          size = .4,
          alpha = .6) +
  geom_sf(data = river$osm_lines,
          inherit.aes = FALSE,
          color = "red",
          size = .2,
          alpha = .5) +
  coord_sf(xlim = c(-69.50, -69.05), 
           ylim = c(9.99, 10.20),
           expand = FALSE) 

Now you can try it and to make the streetmap of you city. If you need help feel free tell me.