Skill: World Map (R)
Category
Hiplot
When to Use
Create a World Map using R with the Hiplot platform's approach. Suitable for biomedical data visualization with publication-quality output.
Required R Packages
- RColorBrewer
- data.table
- ggplot2
- jsonlite
Minimal Reproducible Code
# Load packages
library(RColorBrewer)
library(data.table)
library(ggplot2)
library(jsonlite)
# Prepare data
# Load data
data <- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/map-world/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)
dt_map <- readRDS(url("https://download.hiplot.cn/ui/basic/map-world/world.rds"))
# Convert data structure
dt_map$Value <- data$death_rate[match(dt_map$ENG_NAME, data$region)]
# View data
head(data)
# Create visualization
# World Map
p <- ggplot(dt_map) +
geom_polygon(aes(x = long, y = lat, group = group, fill = Value),
alpha = 0.9, size = 0.5) +
geom_path(aes(x = long, y = lat, group = group),
color = "black", size = 0.2) +
coord_fixed() +
scale_fill_gradientn(
colours = colorRampPalette(rev(brewer.pal(11,"RdYlBu")))(500),
na.value = "grey10",
limits = c(0, max(dt_map$Value) * 1.2)) +
ggtitle("World Map Plot") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5),
legend.position = "bottom", legend.direction = "horizontal")
p
Key Parameters
x: Mapslongto the x aestheticy: Mapslatto the y aestheticgroup: Mapsgroupto the group aestheticfill: MapsValueto the fill aestheticalpha: Controls transparency (0 = fully transparent, 1 = opaque)position: Position adjustment (identity, dodge, stack, fill)theme: Plot theme; tutorial usestheme_minimal()
Tips
- Use
theme_minimal()ortheme_bw()for clean, publication-ready plots - Customize color scales with
scale_fill_manual()orscale_color_brewer() - See the full tutorial for additional customization options and advanced examples