/
04_hexagons.R
67 lines (61 loc) · 2.32 KB
/
04_hexagons.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# https://www.worldpop.org/geodata/summary?id=41389
# https://cartogrid.vercel.app/
library(tidyverse)
library(sf)
pop_tbl <- read_csv("data/deu_pd_2020_1km_ASCII_XYZ.csv")
pop_sf <- pop_tbl %>%
st_as_sf(coords = c("X", "Y"), agr = "constant")
st_crs(pop_sf) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
de <- rgdal::readOGR("data/Germany.geojson", "Germany")
de_df <- fortify(de)
de_sf <- de_df %>%
st_as_sf(coords = c("long", "lat"), agr = "constant") %>%
group_by(id) %>%
summarise(do_union = FALSE) %>%
st_cast("POLYGON")
st_crs(de_sf) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
tmp <- st_intersects(de_sf, pop_sf)
hex_dens <- sapply(tmp, function(x) {
mean(pop_tbl$Z[x], na.rm = TRUE)
})
de_sf$pop_dens <- hex_dens
p <- ggplot() +
geom_sf(data = de_sf, aes(fill = pop_dens), color = "white", size = 0.1) +
scale_fill_distiller(
palette = 3, direction = 1, trans = "sqrt", name = "",
breaks = range(de_sf$pop_dens),
labels = c("low", "high")
) +
theme_void() +
theme(
legend.position = "top",
plot.title = element_text(
color = "#8856a7", hjust = 0.5, size = 64, margin = margin(t = 10, b = 28),
family = "Lobster Two", face = "bold"
),
plot.subtitle = element_text(
color = "#8856a7", hjust = 0.5, size = 56, family = "Open Sans",
margin = margin(b = 10)
),
plot.caption = element_text(color = "#8856a7", size = 38, family = "Open Sans", margin = margin(b = 10)),
plot.background = element_rect(fill = "grey85", size = 5)
) +
guides(fill = guide_colourbar(
barwidth = 35, barheight = 2.0, ticks = FALSE,
frame.colour = "white", title.position = "top",
label.theme = element_text(size = 34, colour = "#8856a7", family = "Open Sans")
)) +
labs(
title = "Population Density of Germany",
subtitle = "aggregated on 1km x 1km hexagon tiles",
caption = "@schochastics "
) +
annotate("text",
x = 10.5, y = 47.0, label = "population data from worldpop.org,\nhex grid created with cartogrid.vercel.app",
family = "Open Sans", color = "grey50", size = 10
)
ragg::agg_png("04_hexagons.png", width = 800 * 2, height = 1500 * 2, units = "px")
p
dev.off()
cmd <- paste0("convert ", '"', "04_hexagons.png", '"', ' -set filename:base "%[base]" -trim +repage "%[filename:base].png"')
system(cmd)