library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
# Load the world map as an sf object
<- ne_countries(scale = "medium", returnclass = "sf")
world # Define the resolution of the grid (e.g., 5 degrees)
<- 3
resolution
# Create a grid of longitude and latitude points
<- expand.grid(lon = seq(-180, 180, by = resolution),
grid lat = seq(-90, 90, by = resolution))
# Convert the grid to an sf object
<- st_as_sf(grid, coords = c("lon", "lat"), crs = 4326) grid_sf
# It takes long time to run
# Crop the grid points so that only points within the world map polygons remain
<- st_intersection(grid_sf, world) grid_cropped
library(ggthemes)
library(ggspatial)
# Plot
<- ggplot() +
map geom_sf(data = grid_cropped,
linetype = "dotdash",
shape = 21,
stroke = 0.2,
fill = NA) +
geom_sf(data = grid_cropped,
aes(color = sovereignt),
show.legend = F,
size = 0.3) +
scale_color_viridis_d(option = "F",
begin = 0.1,
end = 0.9) + # Adjusts color gradient for better contrast
coord_sf(crs = sf::st_crs("+proj=robin")) +
::theme_map()
ggthemes map
+
map # add a north arrow
::annotation_north_arrow(location = "br") +
ggspatial# add a title and a caption
labs(title = "Cropped Grid of Points on World Map",
caption = "Source: Natural Earth | #30DayMapChallenge 2024 Day1 | @fgazzelloni") +
# adjust the appearance of the plot
theme(text = element_text(family = "Courier"),
plot.title = element_text(size = 16, color = "grey20", face = "bold"),
plot.caption = element_text(size = 8, color = "grey20", face = "bold"),
plot.background = element_rect(fill = NA, color = NA),
panel.background = element_rect(fill = NA, color = NA),
#panel.grid = element_line(color = "grey90"),
legend.position = "none")