Skip to content

Instantly share code, notes, and snippets.

@eliocamp
Created November 13, 2023 16:34
Show Gist options
  • Save eliocamp/20502a5be123298eb4bf88daf086bf5b to your computer and use it in GitHub Desktop.
Save eliocamp/20502a5be123298eb4bf88daf086bf5b to your computer and use it in GitHub Desktop.
buenos aires look
library(ggplot2)
library(dplyr)
library(extrafont)
#
# extrafont::font_import("~/Downloads/")
CHANEWEI <- "Chalet-NewYorkNineteenEighty"
# Download data
url <- "https://apitransporte.buenosaires.gob.ar/ecobici/gbfs/stationInformation"
id <- "956bc19f96ba498099dd33bb8dbaebb4"
secret <- Sys.getenv("BICI_SECRET", unset = "")
estaciones <- httr::GET(url,
query = list(client_id = id,
client_secret = secret)) |>
httr::content( as = "text", encoding = "UTF-8") |>
jsonlite::fromJSON() |>
_$data |>
_$stations
mapa_barrios <- sf::st_read("https://cdn.buenosaires.gob.ar/datosabiertos/datasets/ministerio-de-educacion/barrios/barrios.geojson")
proj <- "+proj=lcc +lon_0=-58 +lat_1=-34.55 +lat_2=-34.70"
theme_ba <- function() {
list(
# Normal theme stuff
theme_void(base_size = 14),
theme(text = element_text(family = CHANEWEI, color = "#1d1c1a"),
legend.position = "bottom"
),
# Placeholder structure with special class "ba"
structure(.Data = list(), class = "ba")
)
}
# The "ba" object adds a new class to the plot when added to it
ggplot_add.ba <- function(object, plot, object_name) {
class(plot) <- c("ba_plot", class(plot))
return(plot)
}
# The "ba_plot" object has special ggplot_build and ggplot_gtable
# methods.
# The first method does nothing except to add a new class
# to the built plot. This then uses the custom ggplot_gtable method
ggplot_build.ba_plot <- function(plot) {
gb <- NextMethod("ggplot_build")
class(gb) <- c("ba_plot", class(gb))
return(gb)
}
# The ggplot_gtable method adds a line above the title panel.
ggplot_gtable.ba_plot <- function(data) {
# First, do all the normal ggplot_gtable stuff
gt <- NextMethod("ggplot_gtable")
# Now, add the line
line <- grid::linesGrob(x = c(0, 1),
y = c(1, 1),
gp = grid::gpar(col = "#fdd306",
lwd = 10))
panels <- gt$layout$name == "title"
this_panel <- gt$layout[panels, ]
gt <- gtable::gtable_add_grob(gt, line, t = this_panel$t, l = this_panel$l)
return(gt)
}
estaciones |>
select(lon, lat, capacidad = capacity) |>
sf::st_as_sf(coords = c("lon", "lat"), crs = sf::st_crs(mapa_barrios)) |>
sf::st_join(mapa_barrios, left = FALSE) |>
mutate(capacidad = ifelse(is.na(capacidad), 0, capacidad)) |>
group_by(BARRIO) |>
summarise(estaciones = n(),
capacidad = sum(capacidad)) |>
as.data.frame() |>
full_join(mapa_barrios, by = "BARRIO") |>
mutate(area2 = as.numeric(sf::st_area(geometry.y))/1000^2) |>
mutate(capacidad = ifelse(is.na(capacidad), 0, capacidad)) |>
mutate(lon = sf::st_coordinates(sf::st_point_on_surface(geometry.y))[, 1],
lat = sf::st_coordinates(sf::st_point_on_surface(geometry.y))[, 2]) |>
ggplot(aes(fill = capacidad/area2, geometry = geometry.y)) +
geom_sf(color = "#34485e", linewidth = 0.1) +
ggforce::geom_mark_circle(aes(lon, lat, filter = capacidad == 0, label = BARRIO), color = NA,
label.family = CHANEWEI, label.colour = "#34485e",
description = "No hay ni una sola estación de bicicletas en todo el barrio. ") +
scale_fill_steps("Capacidad de bicicletas por km^2",
low = "white", high = "#fdd306",
breaks = \(x) unique(c(0, 1, pretty(c(1, max(x)), n = 7))),
guide = guide_colorsteps(barwidth = 20, barheight = 0.5,
title.position = "top", title.hjust = 0.5,
frame.colour = "#34485e")) +
coord_sf(crs = proj) +
labs(title = "Disfrutá de la Ciudad en bicicleta",
subtitle = "... si no vivís en el sur") +
theme_ba()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment