【R绘图】绘制世界地图和中国地图

2024-01-24  本文已影响0人  花生学生信

绘制世界地图

library(maps)
library(ggplot2)

# 获取世界地图数据
world_map <- map_data("world")

# 绘制地图
ggplot() +
  geom_map(data = world_map, map = world_map,
           aes(x = long, y = lat, map_id = region),
           color = "black", fill = "white") +
  theme_void()
世界地图

中国地图

china <- ne_countries(country = "china", returnclass = "sf")

# 获取台湾岛地图数据
taiwan <- ne_countries(country = "taiwan", returnclass = "sf")

# 绘制地图
ggplot() +
  geom_sf(data = china, color = "black", fill = "white") +
  geom_sf(data = taiwan, color = "red", fill = "red") +
  geom_label(x = 120.9, y = 23.75, label = "台湾", color = "black", size = 3) +
  theme_void()

image.png

把省份加上

library(rnaturalearth)
library(ggplot2)

# 获取中国地图数据
china <- ne_countries(country = "china", returnclass = "sf")

# 获取中国省份地图数据
china_provinces <- ne_states(country = "china", returnclass = "sf")

# 绘制地图
ggplot() +
  geom_sf(data = china_provinces, color = "black", fill = "white") +
  theme_void()+geom_sf(data = taiwan, color = "red", fill = "red") +
  geom_label(x = 120.9, y = 23.75, label = "台湾", color = "black", size = 3) +
  theme_void()
上一篇下一篇

猜你喜欢

热点阅读