R语言 如何修复有关来自传单的addPolygon()的错误

mdfafbf1  于 2022-12-25  发布在  Go
关注(0)|答案(2)|浏览(99)

我目前正在努力使用leaflet创建交互式Map的空间 Dataframe 。在R中,我尝试了许多不同的多线程解决方案,但我的addPolygon()似乎根本不起作用。有人能帮我找出问题所在吗?我以简单格式(def_rep)复制了我的数据集,空间数据位于状态级别,为此我调用了tigris::states()。然后,我将 Dataframe 合并到merged_df_rep中,并继续设置leaflet()。但我总是得到相同的错误:UseMethod(“元数据”)中的错误:没有适用于'metaData'的方法应用于类“c('sfc_MULTIPOLYGON','sfc')"的对象。非常感谢您的任何帮助或指示我做错了什么!
下面是我的代码和一个示例df:

library(ggmap)
library(tidygeocoder)
library(tigris)
library(sf)
library(htmltools)
library(labelled)
library(leaflet) 
library(tidyverse)



NAME <- c("West Virginia", "Florida", "Illinois", "Minnesota","Maryland", "Rhode Island", "Idaho", "New Hampshire","North Carolina",       "Vermont", "Connecticut", "Delaware","New Mexico", "California", "New Jersey", "Wisconsin", "Oregon", "Nebraska","Pennsylvania","Washington",           "Louisiana", "Georgia", "Alabama" , "Utah","Ohio","Texas", "Colorado", "South Carolina","Oklahoma" , "Tennessee", "Wyoming", "Hawaii", "North Dakota", "Kentucky",  "Maine" , "New York", "Nevada" ,  "Alaska" , "Michigan", "Arkansas","Mississippi","Missouri", "Montana" ,"Kansas", "Indiana", "South Dakota","Massachusetts", "Virginia", "District of Columbia", "Iowa","Arizona")
values <- c(1000:1050)

df_rep <- data.frame(NAME, values)

map_df <- states()


problems <- anti_join(map_df,df_rep, by = "NAME")
head(problems[,1:7])

###dropping the problems

map_df_clean<-map_df[!(map_df$NAME=="United States Virgin Islands" | map_df$NAME=="Commonwealth of the Northern Mariana Islands"| map_df$NAME=="Guam"| map_df$NAME=="Puerto Rico"| map_df$NAME=="American Samoa"),]


merged_df_rep <- left_join(map_df_clean, df_rep, by = "NAME")

##try to transform the data
merged_df_rep <- st_transform(merged_df_rep, "+proj=longlat +datum=WGS84")

pal <- colorNumeric("Greens", domain = merged_df_rep$values) ##create a color palette

#states_merged_df <- sf::as_Spatial(states_merged_df)

popup_sb <- paste0("Total values for" ,merged_df_rep$NAME, "Number:", as.character(merged_df_rep$values)) ##create the correct popup

merged_df_rep %>% 
leaflet() %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  setView(~-98.483330,38, .712046, zoom = 4) %>% 
  addPolygons(
    data = merged_df_rep$geometry,
              fillColor = ~pal(merged_df_us_1$n_response),
              fillOpacity = .7,
              weight = .2, 
              smoothFactor = .2,
              popup = ~popup_sb) %>% 
  addLegend(pal = pal, 
            values = merged_df_rep$values,
            position = "bottomright",
            title = "Responses")
ztigrdn8

ztigrdn81#

首先,您没有向我们提供merged_df_us_1$n_response,因此我注解掉了fillColor,其次

setView(~-98.483330,38, .712046, zoom = 4) %>%

第三件事是,当leaflet需要sf对象时,不能只提供geometry列作为数据:
x一个一个一个一个x一个一个二个x

问候你格热戈兹

r55awzrz

r55awzrz2#

这个错误指出有些东西不能处理 sfc 类,sfc是简单要素的几何列,在这个例子中是merged_df_rep$geometrydata = merged_df_rep就可以工作,但是你已经把sf对象通过管道传输到leaflet()并设置了默认的默认数据对象,没有必要在单独的层中用相同的数据集覆盖它。
公式接口似乎也有些混乱,传单的这一部分确实有点混乱,但它主要用于引用作为data参数传递的数据集的变量。https://rstudio.github.io/leaflet/map_widget.html#the-formula-interface

library(tigris)
library(sf)
library(leaflet)
library(dplyr)

NAME <- c("West Virginia", "Florida", "Illinois", "Minnesota", "Maryland", "Rhode Island", "Idaho", "New Hampshire", "North Carolina", "Vermont", "Connecticut", "Delaware", "New Mexico", "California", "New Jersey", "Wisconsin", "Oregon", "Nebraska", "Pennsylvania", "Washington", "Louisiana", "Georgia", "Alabama", "Utah", "Ohio", "Texas", "Colorado", "South Carolina", "Oklahoma", "Tennessee", "Wyoming", "Hawaii", "North Dakota", "Kentucky", "Maine", "New York", "Nevada", "Alaska", "Michigan", "Arkansas", "Mississippi", "Missouri", "Montana", "Kansas", "Indiana", "South Dakota", "Massachusetts", "Virginia", "District of Columbia", "Iowa", "Arizona")
values <- c(1000:1050)
df_rep <- data.frame(NAME, values)

map_df <- states() %>% st_transform("WGS84")
merged_df_rep <- inner_join(map_df, df_rep, by = "NAME")

pal <- colorNumeric("Greens", domain = merged_df_rep$values) ## create a color palette

leaflet(merged_df_rep) %>%
  addProviderTiles("CartoDB.Positron") %>%
  setView(-98.483330, 38, .712046, zoom = 4) %>%
  addPolygons(
    fillColor = ~ pal(values),
    fillOpacity = .7,
    weight = .2,
    smoothFactor = .2,
    popup = ~ paste0("Total values for ", NAME, "<br> Number: ", as.character(values))
  ) %>%
  addLegend(
    pal = pal,
    values = ~values,
    position = "bottomright",
    title = "Responses"
  )

相关问题