You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R语言plot_geo绘制伊朗地图时如何添加省份名称标签

实现代码

library(sf)
library(dplyr)
library(plotly)

# 读取伊朗省份边界数据
iran <- st_read("D:\\rm\\SMI\\www\\gis\\irn_admbnda_adm1_unhcr_20190514.shp")

# 构造指标与颜色映射规则
dat <-data.frame(
id = 1:31,
state = iran$ADM1_EN ,
pr = c(530,-42,1673,75,206,544,1490,118,75,
       40,105,191,111,810, 609,425,418,550, 40, 425, -54,-50,
       16, 18, 133,425, -30, 241,63, 191,100))
dat<-dat %>% mutate(color_province = case_when(pr <= 50 ~ 'green',
                                           pr > 150 ~ 'red',
                                           TRUE ~ 'yellow'))

# 提取各省几何质心的经纬度坐标
iran <- st_centroid(iran) %>%
  mutate(
    lon = st_coordinates(.)[,1],
    lat = st_coordinates(.)[,2]
  ) %>% left_join(dat, by = c("ADM1_EN" = "state"))

# 绘制带省份名称标签的地图
plot_geo(iran) %>%
  add_sf(color = ~color_province, showlegend = FALSE) %>%
  add_text(
    x = ~lon,
    y = ~lat,
    text = ~ADM1_EN,
    textfont = list(color = "black", size = 10),
    showlegend = FALSE
  )

可调参数说明

  • 若出现标签重叠问题,可在add_text中新增textposition参数调整标签相对质心的偏移位置,可选值为top/bottom/left/right等
  • 可修改textfont内的size和color参数,调整标签的字号和字体颜色

内容的提问来源于stack exchange,提问作者Masoud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 07:15:08