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

R语言Plotly无法渲染GeoJSON制作Choropleth地图问题

问题:Plotly R无法渲染GeoJSON医疗区地图

问题描述

我正尝试用R语言的Plotly库制作Choropleth地图,参考了Plotly官方的Choropleth地图教程,使用的GeoJSON是犹他州医疗区的公开地理数据。目前遇到的核心问题是Plotly无法渲染GeoJSON文件中的医疗区形状(暂时不关注z参数指定的颜色维度,仅需要成功渲染地图轮廓)。

我对地理空间数据经验有限,但已确认GeoJSON文件的引用逻辑无误——通过geojson$features[[1]]$properties$DISTNAME可正常访问DISTNAME字段,且该字段的字符格式与我的元数据完全匹配。我遇到的问题和Stack Overflow上的一个类似问题情况一致,但该帖子中的解决方案并未解决我的问题。

代码示例

data <- data.frame(
  DISTNAME <- c("Southwest Utah", "Central Utah", "Southeast Utah", "Utah County", "Bear River", "Summit County", "TriCounty",      
                "Tooele County", "Weber-Morgan", "Wasatch County", "Davis County", "Salt Lake County", "San Juan"),
  ID_NUM <- c(6.0, 2.0, 5.1, 10.0, 1.0, 7.0, 9.0, 8.0, 12.0, 11.0, 3.0, 4.0, 13.0),
  COLOR4 <- c(1,2,3,1,4,3,4,3,2,2,1,4,4),
  type <- c("Unknown","EMM83.1","Unknown","EMM1.0","EMM11.0","EMM11.0","EMM1.0","EMM60.2","Unknown","Unknown","Unknown","EMM1.0", "Unknown")
)

download.file("https://services1.arcgis.com/99lidPhWCzftIe9K/arcgis/rest/services/HealthDistricts/FeatureServer/replicafilescache/HealthDistricts_-7032148845298891659.geojson",destfile = "HealthDistricts.geojson")
geojson <- rjson::fromJSON(file = "HealthDistricts.geojson") # 读取JSON数据

library(plotly) # 加载Plotly库

fig <- plot_ly() 
fig <- fig %>% add_trace(
  type = "choropleth",
  geojson = geojson,
  locations = data$DISTNAME,
  z = data$type,
  featureidkey = "properties.DISTNAME"
)

fig

求助需求

恳请各位提供技术指导,解决Plotly无法渲染GeoJSON中医疗区形状的问题。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 03:49:55