Population of the Czech Republic from the 2011 census, 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(readxl)
library(dplyr)
library(httr)
tf <- tempfile(fileext = ".xls") # a temporary xls file
GET("https://raw.githubusercontent.com/jlacko/RCzechia/master/data-raw/zvcr034.xls",
write_disk(tf))
## Response [https://raw.githubusercontent.com/jlacko/RCzechia/master/data-raw/zvcr034.xls]
## Date: 2024-11-25 14:10
## Status: 200
## Content-Type: application/octet-stream
## Size: 44.5 kB
## <ON DISK> /tmp/Rtmpn3okFy/file20e11aee97ca.xls
src <- read_excel(tf, range = "Data!B5:C97") # read in with original column names
colnames(src) <- c("NAZ_LAU1", "obyvatel") # meaningful names instead of the original ones
src <- src %>%
mutate(obyvatel = as.double(obyvatel)) %>%
# convert from text to number
mutate(NAZ_LAU1 = ifelse(NAZ_LAU1 == "Hlavní město Praha", "Praha", NAZ_LAU1))
# rename Prague (from The Capital to a regular city)
okresni_data <- RCzechia::okresy("low") %>% # data shapefile
inner_join(src, by = "NAZ_LAU1")
# 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 = RCzechia::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 = element_text(hjust = 1),
legend.title = element_text(hjust = 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)
borders <- RCzechia::republika("low")
rivers <- subset(RCzechia::reky(), Major == T)
mista <- data.frame(misto = c("Kramářova vila",
"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
POI <- RCzechia::geocode(mista$adresa)
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)
obce <- RCzechia::obce_polygony()
praha <- subset(obce, NAZ_OBEC == "Praha")
brno <- subset(obce, NAZ_OBEC == "Brno")
vzdalenost <- sf::st_distance(praha, brno) %>%
units::set_units("kilometers") # easier to interpret than meters, miles or decimal degrees..
# report results
print(vzdalenost[1])
## 152.4642 [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
brno <- RCzechia::okresy() %>%
dplyr::filter(KOD_LAU1 == "CZ0642")
# calculate centroid
pupek_brna <- brno %>%
sf::st_transform(5514) %>% # planar CRS (eastings & northings)
sf::st_centroid(brno) # calculate central point of a polygon
# the revgeo() function takes a sf points data frame and returns it back
# with address data in "revgeocoded" column
adresa_pupku <- RCzechia::revgeo(pupek_brna) %>%
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.
Since Stamen Toner basemap no longer sparkles joy I have found a new favorite - the Positron by CartoDB.
Note: it is technically impossible to make html in vignette interactive (and for good reasons). 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)
# map metrics - number of unemployed in October 2020
metrika <- czso::czso_get_table("250169r20") %>%
filter(obdobi == "20201031" & vuk == "NEZ0004")
podklad <- RCzechia::obce_polygony() %>% # obce_polygony = municipalities in RCzechia package
inner_join(metrika, by = c("KOD_OBEC" = "uzemi_kod")) %>% # linking by key
filter(KOD_CZNUTS3 == "CZ071") # Olomoucký kraj
pal <- colorNumeric(palette = "viridis", domain = podklad$hodnota)
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = podklad,
fillColor = ~pal(hodnota),
fillOpacity = 0.75,
color = NA)