中国地图

作者

[编辑] 郑虎;

[审核] .

注记

Hiplot 网站

本页面为 Hiplot China Map 插件的源码版本教程,您也可以使用 Hiplot 网站实现无代码绘图,更多信息请查看以下链接:

https://hiplot.cn/basic/map-china?lang=zh_cn

环境配置

  • 系统: Cross-platform (Linux/MacOS/Windows)

  • 编程语言: R

  • 依赖包: ggplot2; RColorBrewer

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

# 加载包
library(ggplot2)
library(RColorBrewer)

数据准备

# 加载数据
data <- read.delim("files/Hiplot/100-map-china-data.txt", header = T)
dt_map <- readRDS("files/Hiplot/china.rds")

# 整理数据格式
dt_map$Value <- data$value[match(dt_map$FCNAME, data$name)]

# 查看数据
head(data)
    name value
1 安徽省  6118
2 福建省  3581
3 甘肃省  2617
4 广东省  9449
5 贵州省  3762
6 海南省   845

可视化

# 中国地图
p <- ggplot(dt_map, aes(x = long, y = lat, group = group, fill = Value)) +
  labs(fill = "Value") +
  geom_polygon() +
  geom_path() +
  scale_fill_gradientn(
    colours = colorRampPalette(rev(brewer.pal(11,"RdYlBu")))(500),
    na.value = "grey10",
    limits = c(0, max(dt_map$Value) * 1.2)) +
    ggtitle("China Map Plot") +
  theme_minimal()

p
图 1: 中国地图