library(tidyverse)

world <- map_data("world") # 画图数据
world[which(world$region=='Taiwan'),'region'] <- "China" # 中国台湾

# 各国标签
labels <- read.csv('map.csv') # 下载标签数据后读取

# 整合画图数据与标签数据
world$value <- 0
for (i in 1:nrow(labels)) {
  world[which(world$region==labels$region[i] ),'value'] <- labels[which(labels$region==labels$region[i]),'value']
};rm(i)

# 画图
library(ggplot2)
ggplot() +
  geom_polygon(data = world, # 画图数据
               aes( x = long, y = lat,group=group,fill=value), # 经、纬度列,依据数值填充颜色
               color="white") + # 界线颜色
  scale_fill_gradient(name = "value", # 数值列
  low= 'gray', high= 'red' )+ # 低值与高值颜色
  theme_void()+ # 主题
  theme(legend.position = "right")+ # 图例位置(top right left bottom none)
  geom_text(data = labels , # 标签数据
            aes(x = long, y= lat, label =label), # 经、纬度坐标及标签列
            colour = "black",  size = 2) # 标签颜色、字号

# save(world,labels,file = 'worldmap.Rdata')

download_worldmap 标签示例数据

Avatar photo

作者 xian

发表回复