#install.packages(c("leaflet", "rnaturalearth", "rnaturalearthdata", "sf", "dplyr"))
5 Install necessary packages
6 Load libraries
library(leaflet)
library(rnaturalearth)
library(rnaturalearthdata)
Attaching package: 'rnaturalearthdata'
The following object is masked from 'package:rnaturalearth':
countries110
library(sf)
Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
7 Get world countries shapefile
<- ne_countries(scale = "medium", returnclass = "sf") world
8 List of countries to highlight
<- c( "China", "India", "Japan", "Pakistan", "Singapore", "Democratic Republic of the Congo", "Ethiopia", "Nigeria", "Senegal", "Sierra Leone", "South Africa", "Tanzania", "Gambia", "Uganda", "Zambia", "Zimbabwe", "Germany", "United Kingdom" ) highlighted_countries
9 Create a new column to flag highlighted countries
<- world %>% mutate(highlight = ifelse(admin %in% highlighted_countries, "Yes", "No")) world
10 Make the map
10.1 Leaflet Map
# Make the map
leaflet(world) %>%
addTiles() %>%
addPolygons(
fillColor = ~ifelse(highlight == "Yes", "darkred", "lightgray"),
weight = 1,
color = "white",
fillOpacity = 0.7,
label = ~admin
)
10.2 Robinson Projection
# Load packages
library(sf)
library(rnaturalearth)
library(ggplot2)
library(dplyr)
# Load country shapes
<- ne_countries(scale = "medium", returnclass = "sf")
world
# Your countries
<- c(
highlighted_countries "China", "India", "Japan", "Pakistan", "Singapore",
"Democratic Republic of the Congo", "Ethiopia", "Nigeria", "Senegal",
"Sierra Leone", "South Africa", "Tanzania", "Gambia", "Uganda",
"Zambia", "Zimbabwe", "Germany", "United Kingdom"
)
# Flag the selected countries
<- world %>%
world mutate(highlight = ifelse(admin %in% highlighted_countries, "Yes", "No"))
# Plot with Robinson projection
ggplot(world) +
geom_sf(aes(fill = highlight), color = "white", size = 0.2) +
scale_fill_manual(values = c("Yes" = "red", "No" = "gray90")) +
coord_sf(crs = "+proj=robin") + # Robinson projection
theme_minimal()
10.3 Equal Earth Projection
# Load required packages
library(sf)
library(rnaturalearth)
library(ggplot2)
library(dplyr)
# Load world countries shapefile
<- ne_countries(scale = "medium", returnclass = "sf")
world
# List of countries to highlight
<- c(
highlighted_countries "India", "Japan", "Pakistan", "Singapore",
"Democratic Republic of the Congo", "Ethiopia", "Nigeria", "Senegal",
"Sierra Leone", "South Africa", "Tanzania", "Gambia", "Uganda",
"Zambia", "Zimbabwe", "Germany", "United Kingdom"
)
# Flag the countries
<- world %>%
world mutate(highlight = ifelse(admin %in% highlighted_countries, "Yes", "No"))
# Plot with Equal Earth projection
ggplot(world) +
geom_sf(aes(fill = highlight), color = "white", size = 0.2) +
scale_fill_manual(values = c("Yes" = "#D7191C", "No" = "gray90")) +
coord_sf(crs = "+proj=eqearth") + # Equal Earth projection
theme_minimal()+
theme(legend.position = "none")