R/leaflet - 绘制众多多边形

3
我正在尝试使用leaflet包绘制多边形,但我不明白出了什么问题。
我使用的shapefile可以在这里找到:https://www.data.gouv.fr/en/datasets/fond-de-carte-des-codes-postaux/
library(leaflet)
library(rgdal)
df <- readOGR("C:/Users/me/codes_postaux","codes_postaux_region")
plot(df)

输出的shapefile

这个shapefile看起来没问题,我使用的代码相当简单。但是我只得到了地图作为输出,没有多边形。我一直在为这个问题苦苦挣扎,如果有人能帮我解决这个问题,我将不胜感激。

map <- leaflet(df) %>%
  addProviderTiles("CartoDB.Positron")%>%
  fitBounds(10,38,10,55) %>% 
  addPolygons(fillOpacity = 0.8, color = "Blue", weight = 1)

map

leaflet


它能用这个数据吗:df <- raster::getData("GADM", country = "FRA", level = 4)?对我来说可以,只是想知道是你的数据还是某个包相关的问题。 - TimSalabim
1个回答

4
看一下 df@proj4stringplot(df); axis(1); axis(2) 的输出。你的 shapefile 使用了特定的 CRS。需要将你的 SpatialPolygonsDataFrame 转换为通用的 CRSobj(我从这里获取了 CRS 代码:R 中的 Leaflet:栅格图像)。
library(sp)

pj <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
df2 <- spTransform(df, pj)

map2 <- leaflet(df2) %>%
  addProviderTiles("CartoDB.Positron")%>%
  fitBounds(10,38,10,55) %>% 
  addPolygons(fillOpacity = 0.8, color = "Blue", weight = 1)
map2

enter image description here


谢谢,这个很好用。你能否请指明在CRS()中如何找到投影参数? - Pierre Dudek
@PierreDudek; 我知道它是最常见的,所以我用了它(我回答时没有搜索)。但现在我在 R 的传单描述中找到了 CRS,并将其重写到其中(它们的差异只是一种表达方式)(参见:R 的 Leaflet:光栅图像)。 - cuttlefish44

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接