R:地图示例

rm(list = ls()) # 清除所有变量
setwd("C:\\Users\\Administrator\\Desktop\\新建文件夹\\circlize")
library(sf) 
library(ggplot2)
theme_set(theme_bw())
library(rnaturalearth)
library(rnaturalearthdata)
library(ggspatial)
library(maps)
library(tools)

chn_provinces <- ne_states(country = "china", returnclass = "sf")
chn_provinces_points <- st_centroid(chn_provinces)
chn_provinces_points <- cbind(chn_provinces, st_coordinates(st_centroid(chn_provinces$geometry)))

world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)

# 新乡市的大致中心坐标(已计算)
xinxiang_lon <- (113 + 23/60 + 114 + 59/60) / 2
xinxiang_lat <- (34 + 53/60 + 35 + 50/60) / 2

# 选定的海域位置坐标
sea_lon <- 121.5
sea_lat <- 34

# 获取台湾的数据
taiwan <- world[world$admin == "Taiwan", ]

# 计算台湾的几何中心点,用于标注名称
taiwan_centroid <- st_centroid(taiwan$geometry)

ggplot() +
  geom_sf(data = world, fill= "antiquewhite") +  # 绘制世界地图
  geom_sf(data = chn_provinces, fill = "#FAFAD2") +  # 添加中国各省边界
  geom_sf(data = taiwan, fill = "#FAFAD2") +  # 单独绘制台湾,颜色与中国大陆相同
  geom_text(data= chn_provinces_points, aes(x=X, y=Y, label=name),  # 标注省份名称
            color = "darkred", fontface = "bold", check_overlap = TRUE, size = 2.5) +
  geom_text(data = data.frame(x = st_coordinates(taiwan_centroid)[1], y = st_coordinates(taiwan_centroid)[2], label = "Taiwan"), 
            aes(x = x, y = y, label = label), color = "darkred", fontface = "bold", check_overlap = TRUE, size = 2.5) +  # 添加台湾名称
  geom_segment(aes(x = xinxiang_lon, y = xinxiang_lat, xend = sea_lon, yend = sea_lat), 
               color = "#808000", size = 0.8) +  # 从新乡市到海域位置的指引线
  geom_point(aes(x = xinxiang_lon, y = xinxiang_lat), color = "red", size = 4) +  # 新乡市位置点
  geom_text(aes(x = sea_lon, y = sea_lat, label = "Xinxiang"), 
            color = "black", size = 5, hjust = 0) +  # 将“新乡”标签放置在海域位置
  annotation_scale(location = "br", width_hint = 0.5) +
  annotation_north_arrow(location = "br", which_north = "true", 
                         pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in"),
                         style = north_arrow_fancy_orienteering) +
  coord_sf(xlim = c(105, 133), ylim = c(20, 46), expand = TRUE) +  # 设置视图范围限制在中国
  xlab("Longitude") +  # 设置x轴标题为经度
  ylab("Latitude")  # 设置y轴标题为纬度

# 保存地图为PNG文件,宽度10英寸,高度8英寸,分辨率300 DPI
ggsave(filename = "china_map_with_taiwan.png", width = 10, height = 8, dpi = 600)

 

posted @ 2024-03-01 09:05  王哲MGG_AI  阅读(18)  评论(0编辑  收藏  举报