美文网首页
【R绘图】绘制世界地图和中国地图

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

作者: 花生学生信 | 来源:发表于2024-01-24 19:34 被阅读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()

相关文章

网友评论

      本文标题:【R绘图】绘制世界地图和中国地图

      本文链接:https://www.haomeiwen.com/subject/nwsmodtx.html