Skip to content

Instantly share code, notes, and snippets.

@sarajoha
Created January 19, 2023 01:29
Show Gist options
  • Save sarajoha/5bff3565098ff1ae042f5f5945577704 to your computer and use it in GitHub Desktop.
Save sarajoha/5bff3565098ff1ae042f5f5945577704 to your computer and use it in GitHub Desktop.
Mapa de las calles de una ciudad con R
# tutorial
# http://joshuamccrain.com/tutorials/maps/streets_tutorial.html
# install packages
install.packages(remotes)
install.packages(tidyverse)
install.packages(osmdata) # package for working with streets
install.packages(showtext) # for custom fonts
install.packages(ggmap)
install.packages(rvest)
# load packages
library(remotes)
remotes::install_github("ropensci/osmdata")
library(tidyverse)
library(osmdata)
library(showtext)
library(ggmap)
library(rvest)
# tags disponibles en OSM
available_tags("highway")
# descargar calles
# %>% se llama pipelin, permite anidar llamadas de funciones
# third(second(first(x))) == first(x) %>% second(x) %>% third(x)
# opq es un overpass query que va al Overpass API de OSM
big_streets <- getbb("Caracas")%>%
opq()%>%
add_osm_feature(key = "highway", value = c("motorway", "primary", "motorway_link", "primary_link")) %>%
osmdata_sf()
# mostrar la data en una tabla
View(big_streets[["osm_lines"]])
med_streets <- getbb("Caracas")%>%
opq()%>%
add_osm_feature(key = "highway",
value = c("secondary", "tertiary", "secondary_link", "tertiary_link")) %>%
osmdata_sf()
small_streets <- getbb("Caracas")%>%
opq()%>%
add_osm_feature(key = "highway",
value = c("residential", "living_street",
"unclassified",
"service", "footway"
)) %>%
osmdata_sf()
river <- getbb("Caracas")%>%
opq()%>%
add_osm_feature(key = "waterway", value = "river") %>%
osmdata_sf()
railway <- getbb("Caracas")%>%
opq()%>%
add_osm_feature(key = "railway", value="rail") %>%
osmdata_sf()
# plot the data!
# just the main highways
ggplot() +
geom_sf(data = big_streets$osm_lines,
inherit.aes = FALSE,
color = "black")
# just the river
ggplot() +
geom_sf(data = river$osm_lines,
inherit.aes = FALSE,
color = "black")
# adjusting the zoom
ggplot() +
geom_sf(data = big_streets$osm_lines,
inherit.aes = FALSE,
color = "black") +
geom_sf(data = river$osm_lines,
inherit.aes = FALSE,
color = "black") +
coord_sf(xlim = c(-67.1, -66.7),
ylim = c(10.35, 10.53),
expand = FALSE)
# todo completo
ggplot() +
geom_sf(data = railway$osm_lines,
inherit.aes = FALSE,
color = "black",
size = .2,
linetype="dotdash",
alpha = .5) +
geom_sf(data = med_streets$osm_lines,
inherit.aes = FALSE,
color = "black",
size = .3,
alpha = .5) +
geom_sf(data = small_streets$osm_lines,
inherit.aes = FALSE,
color = "#666666",
size = .2,
alpha = .3) +
geom_sf(data = river$osm_lines,
inherit.aes = FALSE,
color = "steelblue",
size = .8,
alpha = .5) +
geom_sf(data = big_streets$osm_lines,
inherit.aes = FALSE,
color = "black",
size = .5,
alpha = .6) +
coord_sf(xlim = c(-66.93, -66.83),
ylim = c(10.47, 10.52),
expand = FALSE) +
theme_void() + # get rid of background color, grid lines, etc.
theme(plot.title = element_text(size = 20, face="bold", hjust=.5),
plot.subtitle = element_text(size = 8, hjust=.5, margin=margin(2, 0, 5, 0))) +
labs(title = "CARACAS", subtitle = "35.595°N / 82.552°W")
# theme(plot.title = element_text(size = 20, family = "lato", face="bold", hjust=.5),
# plot.subtitle = element_text(family = "lato", size = 8, hjust=.5, margin=margin(2, 0, 5, 0))) +
# labs(title = "CARACAS", subtitle = "35.595°N / 82.552°W")
# TODO:
# centroide de caracas
# import lato font
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment