Skip to content

Instantly share code, notes, and snippets.

@vanatteveldt
Created September 19, 2022 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanatteveldt/942eb55acd4cb811fdb6c9d3d11d46d5 to your computer and use it in GitHub Desktop.
Save vanatteveldt/942eb55acd4cb811fdb6c9d3d11d46d5 to your computer and use it in GitHub Desktop.
# demo: scraping
library(httr)
r = GET('https://opendata.cbs.nl/ODataApi/odata/85275NED/UntypedDataSet')
r$status_code
content(r, as="text") |> str_sub(end=500) |> cat()
d = content(r, as="parsed") |> as_tibble()
# demo: cbs data
# zie: https://opendata.cbs.nl/statline/#/CBS/nl/dataset/70072ned/table
kerncijfers = cbs_get_data("70072ned", RegioS=has_substring("GM"), Perioden="2020JJ00")
colnames(kerncijfers)
d = kerncijfers |> select(statcode=RegioS,
inkomen=ParticuliereHuishoudensExclStudenten_122,
vermogen=ParticuliereHuishoudensExclStudenten_132,
woning=WoningbezitEigenWoning_140,
populatie=TotaleBevolking_1) |>
mutate(woningperc=woning/vermogen)
ggplot(d, aes(x=inkomen, y=vermogen)) + geom_point()
# Demo: join aan metadata
meta = cbs_get_meta("70072ned")
regios = meta$RegioS |> select(statcode=Key, gemeente=Title) |> as_tibble()
inner_join(d, regios)
# demo: map data
library(sf)
map = st_read("https://geodata.nationaalgeoregister.nl/cbsgebiedsindelingen/wfs?request=GetFeature&service=WFS&version=2.0.0&typeName=cbs_gemeente_2020_gegeneraliseerd&outputFormat=json")
head(map)
ggplot(map) + geom_sf() + theme_minimal()
# demo: join en map
inner_join(map, d) |>
ggplot() + geom_sf(aes(fill=inkomen)) + theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment