在传单[r]中绘制sf型对象

问题描述 投票:1回答:1

我想问如何在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

但是使用这种方法无效。

leaflet maps sf r-leaflet
1个回答
0
投票

您只是忘记了在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创建

© www.soinside.com 2019 - 2024. All rights reserved.