Population of the Czech Republic as per the latest census in 2011, per district (okres). The results can be easily accessed from the comfort of your R session using the excellent package {czso} by Petr Bouchal.
As the population distributed highly unevenly a log scale is used.
library(RCzechia)
library(ggplot2)
library(dplyr)
library(czso)
<- czso::czso_get_table("SLDB-VYBER") %>%
src select(uzkod, obyvatel = vse1111) %>%
mutate(obyvatel = as.numeric(obyvatel))
<- RCzechia::okresy("low") %>% # data shapefile
okresni_data inner_join(src, by = c("KOD_OKRES" = "uzkod"))
# key for data connection - note the use of inner (i.e. filtering) join
# report results
ggplot(data = okresni_data) +
geom_sf(aes(fill = obyvatel), colour = NA) +
geom_sf(data = republika("low"), color = "gray30", fill = NA) +
scale_fill_viridis_c(trans = "log", labels = scales::comma) +
labs(title = "Czech population",
fill = "population\n(log scale)") +
theme_bw() +
theme(legend.text.align = 1,
legend.title.align = 0.5)
Drawing a map: three semi-random landmarks on map, with rivers shown for better orientation.
To get the geocoded data frame function RCzechia::geocode()
is used.
library(RCzechia)
library(ggplot2)
library(sf)
<- RCzechia::republika("low")
borders
<- subset(RCzechia::reky(), Major == T)
rivers
<- data.frame(misto = c("Kramářova vila",
mista "Arcibiskupské zahrady v Kroměříži",
"Hrad Bečov nad Teplou"),
adresa = c("Gogolova 212, Praha 1",
"Sněmovní náměstí 1, Kroměříž",
"nám. 5. května 1, Bečov nad Teplou"))
# from a string vector to sf spatial points object
<- RCzechia::geocode(mista$adresa)
POI
class(POI) # in {sf} package format = spatial and data frame
## [1] "sf" "data.frame"
# report results
ggplot() +
geom_sf(data = POI, color = "red", shape = 4, size = 2) +
geom_sf(data = rivers, color = "steelblue", alpha = 0.5) +
geom_sf(data = borders, color = "grey30", fill = NA) +
labs(title = "Very Special Places") +
theme_bw()
Calculate distance between two spatial objects; the sf
package supports (via gdal) point to point, point to polygon and polygon to polygon distances.
Calculating distance from Prague (#1 Czech city) to Brno (#2 Czech city).
library(dplyr)
library(RCzechia)
library(sf)
library(units)
<- RCzechia::obce_polygony()
obce
<- subset(obce, NAZ_OBEC == "Praha")
praha
<- subset(obce, NAZ_OBEC == "Brno")
brno
<- sf::st_distance(praha, brno) %>%
vzdalenost ::set_units("kilometers") # easier to interpret than meters, miles or decimal degrees..
units
# report results
print(vzdalenost[1])
## 152.4636 [kilometers]
The metaphysical center of the Brno City is well known. But where is the geographical center?
The center is calculated using sf::st_centroid()
and reversely geocoded via RCzechia::revgeo()
.
Note the use of reky("Brno")
to provide the parts of Svitava and Svratka relevant to a map of Brno city.
library(dplyr)
library(RCzechia)
library(ggplot2)
library(sf)
# all districts
<- RCzechia::okresy() %>%
brno ::filter(KOD_LAU1 == "CZ0642")
dplyr
# calculate centroid
<- brno %>%
pupek_brna ::st_transform(5514) %>% # planar CRS (eastings & northings)
sf::st_centroid(brno) # calculate central point of a polygon
sf
# the revgeo() function takes a sf points data frame and returns it back
# with address data in "revgeocoded" column
<- RCzechia::revgeo(pupek_brna) %>%
adresa_pupku pull(revgeocoded)
# report results
print(adresa_pupku)
## [1] "Žižkova 513/22, Veveří, 61600 Brno"
ggplot() +
geom_sf(data = pupek_brna, col = "red", shape = 4) +
geom_sf(data = reky("Brno"), color = "skyblue3") +
geom_sf(data = brno, color = "grey50", fill = NA) +
labs(title = "Geographical Center of Brno") +
theme_bw()
Interactive maps are powerful tools for data visualization. They are easy to produce with the leaflet
package.
I found the stamen toner basemap a good company for interactive chloropleths - it gives enough context without distracting from the story of your data.
Note: it is technically impossible to make html in vignette interactive. As a consequence the result of code shown has been replaced by a static screenshot; the code itself is legit.
library(dplyr)
library(RCzechia)
library(leaflet)
library(czso)
# metrika pro mapování - uchazeči za říjen
<- czso::czso_get_table("250169r20") %>%
metrika filter(obdobi == "20201031" & vuk == "NEZ0004")
<- RCzechia::obce_polygony() %>% # obce_polygony = municipalities in RCzechia package
podklad inner_join(metrika, by = c("KOD_OBEC" = "uzemi_kod")) %>% # linking by key
filter(KOD_CZNUTS3 == "CZ071") # Olomoucký kraj
<- colorNumeric(palette = "viridis", domain = podklad$hodnota)
pal
leaflet() %>%
addProviderTiles("Stamen.Toner") %>%
addPolygons(data = podklad,
fillColor = ~pal(hodnota),
fillOpacity = 0.75,
color = NA)
The Kartierung der Flora Mitteleuropas (KFME) grid is a commonly used technique in biogeography of the Central Europe. It uses a grid of 10×6 arc-minutes (in Central European latitudes this translates to near squares), with cells numbered from north to south and west to east.
A selection of the grid cells relevant for faunistical mapping of the Czech Republic is available in the RCzechia package.
This example covers a frequent use case:
RCzechia::geocode()
)sf::st_intersection
)library(RCzechia)
library(ggplot2)
library(dplyr)
library(sf)
<- "Humpolec" # a Czech location, as a string
obec
# geolocate the place
<- RCzechia::geocode(obec) %>%
place filter(type == "Obec")
class(place) # a spatial data frame
## [1] "sf" "data.frame"
# ID of the KFME square containg place geocoded (via spatial join)
<- sf::st_join(RCzechia::KFME_grid(),
ctverec_id left = FALSE) %>% # not left = inner (filtering) join
place, pull(ctverec)
print(paste0("Location found in grid cell number ", ctverec_id, "."))
## [1] "Location found in grid cell number 6458."
# a single KFME square to be highlighted as a polygon
<- KFME_grid() %>%
highlighted_cell filter(ctverec == ctverec_id)
# report results
ggplot() +
geom_sf(data = RCzechia::republika(), size = .85) + # Czech borders
geom_sf(data = highlighted_cell, # a specific KFME cell ...
fill = "limegreen", alpha = .5) + # ... highlighted in lime green
geom_sf(data = KFME_grid(), size = .33, # all KFME grid cells, thin
color = "gray80", fill = NA) + # in gray and without fill
geom_sf(data = place, color = "red", pch = 4) + # X marks the spot!
labs(title = paste("Location", obec, "in grid cell number", ctverec_id)) +
theme_bw()
Understanding the lay of the land is important in many use cases in physical sciences; one of them is interpreting the flow of rivers.
Visualizing the slope & height of terrain is an important first step in understanding it.
Package RCzechia supports two versions of relief visualization:
This example covers the second option.
library(RCzechia)
library(ggplot2)
library(dplyr)
library(raster)
# ggplot does not play nice with {raster} package; a data frame is required
<- vyskopis("rayshaded") %>%
relief as("SpatialPixelsDataFrame") %>% # old style format - {sp}
as_tibble()
# report results
ggplot() +
geom_raster(data = relief, aes(x = x, y = y, alpha = -raytraced), # relief
fill = "gray30", show.legend = F) + # no legend is necessary
geom_sf(data = subset(RCzechia::reky(), Major == T), # major rivers
color = "steelblue", alpha = .7) +
labs(title = "Czech Rivers & Their Basins") +
theme_bw() +
theme(axis.title = element_blank())
Visualizing election results is one of typical use cases of the RCzechia package.
This example uses {rvest}
to scrape the official table of results of the 2020 fall Senate elections from the official volby.cz results, and display a map of the party affiliation of the elected senator.
Note that in order to improve resiliency (a key CRAN requirement) the Wayback Machine snapshot is used instead of the actual page.
Since not all districts were up for election in this cycle two thirds of the map contain NA’s; that is expected behavior.
library(RCzechia)
library(ggplot2)
library(dplyr)
library(rvest)
# official result of elections from Czech Statistical Office
<- "https://web.archive.org/web/20210719204451/https://www.volby.cz/pls/senat/se1111?xjazyk=CZ&xdatum=20201002&xv=7&xt=2" %>%
vysledky ::read_html() %>% # because rvest::html is deprecated
xml2html_nodes(xpath = "//*[@id=\"se1111_t1\"]") %>% # get the table by its xpath
html_table(fill = T) %>%
1]] %>%
.[[::select(OBVOD = Obvod, strana = `Volebnístrana`) %>%
dplyr# pad OBVOD with zero to 2 places to align to RCzechia data format
mutate(OBVOD = stringr::str_pad(OBVOD, 2, side = "left", pad = "0"))
<- RCzechia::senat_obvody("low") %>%
podklad # match by key; left to preserve geometry of off cycle districts (NAs)
left_join(vysledky, by = "OBVOD")
ggplot() +
geom_sf(data = RCzechia::republika(), size = .85) + # Czech borders
geom_sf(data = podklad, aes(fill = strana)) +
labs(title = "Senate elections 2020") +
theme_bw()