我想问如何在sf
的传单中绘制leaflet package
对象,我知道mapview package
可以绘制它,但是我倾向于使用lealfet包。
我在下面提供了示例:
library(leaflet)
library(eurostat)
library(dplyr)
library(ggplot2)
options(readr.default_locale=readr::locale(tz="Europe/Berlin"))
df60 <- get_eurostat_geospatial(resolution = 60)
CE.sf <- df60 %>%
dplyr::filter(LEVL_CODE == 2 & CNTR_CODE %in% c("AT","CZ","DE","HU","PL","SK")) %>%
dplyr::select(NUTS_ID)
plot(CE.sf)
CE.sf %>%
ggplot() +
geom_sf(color = "black", size = 0.4)
CE = sf::as_Spatial(CE.sf)
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data= CE, color = "green")
我需要复制传单中第15行的图,我在这里找到了一些想法:https://gis.stackexchange.com/questions/239118/r-convert-sf-object-back-to-spatialpolygonsdataframe
但是使用这种方法无效。
您只是忘记了在data
函数中设置leaflet()
参数。此外,您不需要将sf
对象转换为sp
格式:
# packages
library(leaflet)
library(eurostat)
library(dplyr)
library(ggplot2)
options(readr.default_locale=readr::locale(tz="Europe/Berlin"))
df60 <- get_eurostat_geospatial(resolution = 60)
#> sf at resolution 1:60 read from local file
CE.sf <- df60 %>%
filter(LEVL_CODE == 2 & CNTR_CODE %in% c("AT","CZ","DE","HU","PL","SK")) %>%
select(NUTS_ID)
plot(CE.sf)
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS9veG4zeWMwLnBuZyJ9” alt =“”>
leaflet(CE.sf) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(color = "green")
由reprex package(v0.3.0)在2020-05-01创建