# Install and load required packages
if (!requireNamespace("sf", quietly = TRUE)) install.packages("sf")
if (!requireNamespace("ggplot2", quietly = TRUE)) install.packages("ggplot2")
library(sf)
library(ggplot2)
library(rnaturalearth)
# Load Europe shapefile data
<- ne_countries(continent = "Europe", returnclass = "sf")
europe
# Reproject the data to a projected CRS (ETRS89 / LAEA Europe)
<- st_transform(europe, crs = 3035)
europe_projected
# Create a hexagonal grid with the new CRS
<- st_make_grid(europe_projected,
hex_grid cellsize = 50000,
square = FALSE) # Adjust cellsize as needed
<- st_sf(geometry = hex_grid)
hex_sf
# Clip the hex grid to the shape of Europe
<- st_intersection(hex_sf, st_union(europe_projected)) hex_europe
# sysfonts::font_families()
# Plot the hexagonal grid map
ggplot(data = hex_europe) +
geom_sf(fill = "lightblue", color = "darkblue") +
labs(title = "Hexagon Map of Europe (ETRS89 / LAEA Europe)",
caption = "Data source: Natural Earth\n#30DayMapChallenge 2024 Day4 | @fgazzelloni") +
theme_minimal() +
theme(text = element_text(family = "serif", color = "navy"),
legend.position = "right",
plot.title = element_text(hjust = 0.5,
face = "bold",
color = "navy",
size = 22),
plot.subtitle = element_text(hjust = 0.5,
color = "navy",
size = 15),
plot.caption = element_text(hjust = 0.5,
color = "navy",
size = 10),
axis.text = element_blank(),
panel.grid = element_line(color="navy")) +
# add a north arrow and a scale bar
::annotation_north_arrow(location = "br") +
ggspatial::annotation_scale(location = "bl", width_hint = 0.5) ggspatial
# save the map as png
ggsave("day4_hexagons.png",
bg = "white",
width = 8, height = 8, units = "in", dpi = 300)