# Install the hdxr package from GitHub
# remotes::install_gitlab("dickoa/rhdx")
Load the required libraries and data
Set the HDX site to “prod” and check the configuration settings.
library(tidyverse)
library(rhdx)
set_rhdx_config(hdx_site = "prod")
get_rhdx_config()
Search for Ukraine data on HDX and list the datasets.
<- search_datasets("ukraine")
dat 2] dat[
Select the second dataset and display the metadata: ukraine-border-crossings
<- dat[2] border_crossing
Display the resources available in the dataset.
<- border_crossing %>%
UKR_data nth(1) %>%
get_resource(1) %>%
read_resource()
%>% head() %>% glimpse() UKR_data
Search for Ukraine administrative boundaries data on HDX.
<- search_datasets("Ukraine administrative boundaries") data2
And select the first dataset and display the metadata: kontur-boundaries-ukraine
<- search_datasets("kontur-boundaries-ukraine")
boundaries 1] boundaries[
<- boundaries[1] kontur_boundaries_ukraine
<- kontur_boundaries_ukraine %>%
resource nth(1) %>%
get_resource(1)
resource
$get_format() resource
<- resource$download()
file_path file_path
file.info(file_path)$size
Unzip the file
library(R.utils)
gunzip(file_path,
destname = decompressed_file,
overwrite = TRUE)
Read the shapefile
library(sf)
<- sf::st_read(decompressed_file) ukraine_boundaries
ukraine_boundaries
<- ggplot(ukraine_boundaries) +
map geom_sf()
map
+
map geom_sf(data = UKR_data,
shape = 4,
color="red",
size=2) +
::theme_map() +
ggthemeslabs(title = "Ukraine Border Crossings",
subtitle = "Data from HDX",
caption = "Source: HDX | #30DayMapChallenge 2024 Day8 |Created by @fgazzelloni",
fill = "Population")
ggplot(ukraine_boundaries) +
geom_sf(aes(fill = population)) +
geom_sf(data = UKR_data,
shape = 4,
color="red",
size=2) +
scale_fill_viridis_c() +
::theme_map() +
ggthemeslabs(title = "Ukraine Border Crossings",
subtitle = "Data from HDX",
caption = "Source: HDX | #30DayMapChallenge 2024 Day8 |Created by @fgazzelloni",
fill = "Population")
ggplot(ukraine_boundaries) +
geom_sf(aes(color = population)) +
geom_sf(data = UKR_data,
shape = 4,
color="red",
size=2) +
scale_color_gradient(low = "navy", high = "gold",) +
::theme_map() +
ggthemeslabs(title = "Ukraine Border Crossings",
subtitle = "Data from HDX",
caption = "Source: HDX | #30DayMapChallenge 2024 Day8 |Created by @fgazzelloni",
fill = "Population")
%>%
ukraine_boundariescount(admin_level)
%>%
ukraine_boundariescount(name_en)
<- ukraine_boundaries%>%
ukraine_pop group_by(name_en) %>%
reframe(geom,population = sum(population)) %>%
arrange(desc(population))
ukraine_pop
%>%
ukraine_pop filter(name_en == "Ukraine") %>%
ggplot()+
geom_sf(aes(geometry=geom))
%>%
ukraine_pop filter(!name_en == "Ukraine") %>%
ggplot()+
geom_sf(aes(geometry=geom, fill = population))
%>%
ukraine_pop filter(!name_en == "Ukraine") %>%
ggplot()+
geom_sf(aes(geometry=geom,
fill = population,
color = population)) +
geom_sf(data = UKR_data,
shape = 4,
color="red",
size=2) +
::theme_map() +
ggthemeslabs(title = "Ukraine Border Crossings",
subtitle = "Data from HDX",
caption = "Source: HDX | #30DayMapChallenge 2024 Day8 |Created by @fgazzelloni",
fill = "Population") +
scale_fill_viridis_c(
name = "Population",
labels = scales::label_comma(),
guide = "colourbar" # Show legend for 'fill'
+
) scale_color_viridis_c(
guide = "none" # Suppress legend for 'color'
)
%>%
ukraine_boundaries filter(!name_en == "Ukraine") %>%
ggplot() +
geom_sf(aes(fill = population)) +
geom_sf(data = UKR_data,
shape = 4,
color="red",
size=2) +
scale_fill_viridis_c(name = "Population",
labels = scales::label_comma(),
guide = "colourbar") +
labs(title = "Ukraine Border Crossings",
subtitle = "Data from HDX",
caption = "Source: HDX | #30DayMapChallenge 2024 Day8 |Created by @fgazzelloni",
fill = "Population") +
::theme_map() +
ggthemestheme(
plot.title = element_text(
size = 24,
face = "bold",
color = "darkred",
hjust = 0.5),
plot.subtitle = element_text(
size = 14,
face = "italic",
color = "gray50",
hjust = 0.5))
%>%
ukraine_boundaries filter(!name_en == "Ukraine") %>%
ggplot() +
geom_sf(aes(fill = population)) +
geom_sf(data = UKR_data,
shape = 4,
color="red",
size=2) +
scale_fill_viridis_c(name = "Population",
labels = scales::label_comma(),
guide = "colourbar") +
labs(title = "Ukraine Border Crossings",
subtitle = "Data from HDX",
caption = "Source: HDX | #30DayMapChallenge 2024 Day8 |Created by @fgazzelloni",
fill = "Population") +
::theme_map() +
ggthemestheme(
plot.title = element_text(
size = 24,
face = "bold",
color = "darkred",
hjust = 0.5),
plot.subtitle = element_text(
size = 14,
face = "italic",
color = "gray50",
hjust = 0.5),
plot.caption = element_text(
size = 10,
face = "italic",
color = "gray50",
hjust = 0.5))
Save the plot as a PNG file
ggsave("day8_hdx.png",
bg = "white",
width = 8, height = 6,
dpi = 300)