传单弹出图与地图不对应

问题描述

我正在尝试使用Leaflet包将Choropleth映射制作为html小部件。我不想为此处理闪亮。我有每个州的死亡时间序列数据。我希望能够单击状态并弹出相应的时间序列图。我已经很亲近了,但是我的问题是,单击状态时弹出的图形与该状态不正确对应。例如,如果您单击“俄亥俄州”,则会弹出西弗吉尼亚州地图。

形状文件数据:https://www.census.gov/cgi-bin/geo/shapefiles/index.php?year=2019&layergroup=States+%28and+equivalent%29

完整数据:https://data.cdc.gov/Case-Surveillance/United-States-COVID-19-Cases-and-Deaths-by-State-o/9mfq-cb36

library(tidyverse)
library(lubridate)
library(readr)
library(leaflet)
library(tigris)
library(rgdal)
library(leafpop)

states <- readOGR(dsn = "tl_2019_us_state",layer = "tl_2019_us_state")

covid_deaths<- read_csv("covid_deaths_usafacts.csv")
Clean_Deaths<- covid_deaths%>%
  select(submission_date,state,tot_cases,new_case,tot_death,new_death)%>%
  filter(new_death>=0)%>%
  mutate(submission_date=as.Date(Clean_Deaths$submission_date,"%m/%d/%Y"))

my_list <- list()  
loop<-for (i in unique(Clean_Deaths$state)) {
state<-Clean_Deaths%>% filter(state==i)
  plot<-ggplot(state,aes(x = submission_date,y = new_death)) + 
    geom_line()+scale_x_date(date_breaks = "1 month",date_labels = "%b")+labs(title = i)
  my_list[[i]] <- plot
}

m1 <- leaflet() %>%
  addTiles() %>%
  setView(lng = -120.5,lat = 44,zoom = 6)%>%
  addpolygons(data = states,fillColor = "red",fillOpacity = 0.6,color = "darkgrey",weight = 1.5,popup = popupGraph(my_list)
              )
  m1

My issue

解决方法

我认为您在condlist中使用州的缩写(例如“ NY”),在True中使用全州名称(例如“纽约”)。

在您的Clean_Deaths$state中,您可以从一个转换为另一个。您的states$NAME循环可以经过filter,它将与您在地图中使用的for相匹配:

states$NAME

这是使用data进行比较的简化版本:

for (i in states$NAME) {
  state<-Clean_Deaths%>% filter(state==state.abb[match(i,state.name)])
  plot<-ggplot(state,aes(x = submission_date,y = new_death)) + 
    geom_line()+scale_x_date(date_breaks = "1 month",date_labels = "%b")+labs(title = i)
  my_list[[i]] <- plot
}

顺便说一句,在此之前的lapply不需要管道中引用的数据帧:

my_list <- lapply(states$NAME,function(i) {
  Clean_Deaths %>%
    filter(state == state.abb[match(i,state.name)]) %>%
    ggplot(aes(x = submission_date,y = new_death)) +
      geom_line() +
      scale_x_date(date_breaks = "1 month",date_labels = "%b") +
      labs(title = i)
})

让我知道这是否可以解决您的问题。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...