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

R语言地图可视化:为geom_sf_text文本添加黑色边框

给地图文本添加黑色描边的解决方案

你可以使用shadowtext包中的geom_sf_shadowtext()函数实现文本黑色边框效果,它不会像geom_sf_label()那样生成背景框,完全匹配你的需求。

操作步骤:

  1. 安装并加载shadowtext包
  2. 将原代码中的geom_sf_text()替换为geom_sf_shadowtext(),配置描边相关参数

修改后的完整代码:

library(dplyr)
library(geodata)
library(ggplot2)
library(sf)
library(shadowtext) # 新增依赖包

gadm_valp_com <- gadm(country="CHL", level=3, path=tempdir()) %>%
  st_as_sf() %>%
  filter(NAME_1 == "Valparaíso")

ggplot(gadm_valp_com) + 
  geom_sf() + # 可省略重复的data参数,ggplot已默认继承数据源
  geom_sf_shadowtext(
    aes(label = NAME_3), 
    size=1, 
    family="lato", 
    colour = "white",
    shadowcolour = "black", # 设置描边颜色为黑色
    shadowsize = 0.1 # 控制描边粗细,可按需调整数值
  )

关键参数说明:

  • shadowcolour: 定义文本描边的颜色,这里设置为黑色
  • shadowsize: 控制描边的粗细,数值越小描边越细,可根据实际显示效果微调

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 12:25:22