China Map 2

Authors

[Editor] Hu Zheng;

[Contributors]

Note

Hiplot website

This page is the tutorial for source code version of the Hiplot China Map 2 plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:

https://hiplot.cn/basic/map-china2?lang=en

Setup

  • System Requirements: Cross-platform (Linux/MacOS/Windows)

  • Programming language: R

  • Dependent packages: ggplot2; RColorBrewer

# Install packages
if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2")
}
if (!requireNamespace("RColorBrewer", quietly = TRUE)) {
  install.packages("RColorBrewer")
}

# Load packages
library(ggplot2)
library(RColorBrewer)

Data Preparation

# Load data
data <- read.delim("files/Hiplot/101-map-china2-data.txt", header = T)
dt_map <- readRDS("files/Hiplot/china.rds")

# Convert data structure
dt_map$Value <- data$value[match(dt_map$FCNAME, data$name)]

# View data
head(data)
    name value
1 ๅฎ‰ๅพฝ็œ  6118
2 ็ฆๅปบ็œ  3581
3 ็”˜่‚ƒ็œ  2617
4 ๅนฟไธœ็œ  9449
5 ่ดตๅทž็œ  3762
6 ๆตทๅ—็œ   845

Visualization

# China Map 2
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) +
  scale_fill_gradientn(
    colours = colorRampPalette(rev(brewer.pal(11,"RdYlBu")))(500),
    breaks = seq(min(data$value), max(data$value), 
                 round((max(data$value)-min(data$value))/7)),
    name = "Color Key",
    guide = guide_legend(
      direction = "vertical", keyheight = unit(1, units = "mm"),
      keywidth = unit(8, units = "mm"),
      title.position = "top", title.hjust = 0.5, label.hjust = 0.5,
      nrow = 1, byrow = T, reverse = F, label.position = "bottom")) +
  theme(text = element_text(color = "#3A3F4A"),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "top",
        legend.text = element_text(size = 4 * 1.5, color = "black"),
        legend.title = element_text(size = 5 * 1.5, color = "black"),
        plot.title = element_text(
          face = "bold", size = 5 * 1.5, hjust = 0.5, 
          margin = margin(t = 4, b = 5), color = "black"),
        plot.background = element_rect(fill = "#FFFFFF", color = "#FFFFFF"),
        panel.background = element_rect(fill = "#FFFFFF", color = NA),
        legend.background = element_rect(fill = "#FFFFFF", color = NA),
        plot.margin = unit(c(1.5, 1.5, 1.5, 1.5), "cm")) +
  labs(x = NULL, y = NULL, title = "China Map")

p
Figureย 1: China Map 2