Javascript 无法将数据带入D3 choropleth地图

Javascript 无法将数据带入D3 choropleth地图,javascript,r,d3.js,Javascript,R,D3.js,我很难将约翰·霍普金斯的冠状病毒数据整合到D3 choropleth中,我是个新手,所以可能缺少一个基本的东西。我在互联网上找到的各种来源的代码大部分都被刷过了,但这基本上是一个自学D3的学习项目,所以如果答案注释良好,我将不胜感激 获取数据的R代码: library(tidyverse) library(tools) dat <- read.csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/css

我很难将约翰·霍普金斯的冠状病毒数据整合到D3 choropleth中,我是个新手,所以可能缺少一个基本的东西。我在互联网上找到的各种来源的代码大部分都被刷过了,但这基本上是一个自学D3的学习项目,所以如果答案注释良好,我将不胜感激

获取数据的R代码:

library(tidyverse)
library(tools)

dat <- read.csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")

states <- dat %>% 
  filter(Country.Region == "US", 
         !grepl(",",Province.State),
         !grepl("Princess",Province.State),
         Province.State != "District of Columbia") %>% 
  pivot_longer(5:ncol(.), names_to = "Date", values_to = "Cases") %>% 
  mutate(Date = str_remove(Date,"X"),
         Date = as.Date(Date, "%m.%d.%y")) %>% 
  filter(Date == max(Date)) %>% 
  select(state = Province.State,
         cases = Cases)

counties <-  dat %>% 
  filter(Country.Region == "US", 
         grepl(",",Province.State),
         !grepl("Princess",Province.State),
         Province.State != "Washington, D.C.") %>% 
  separate(Province.State, c("county", "state"), ", ") %>% 
  mutate(county = str_remove(county, " County")) %>% 
  pivot_longer(6:ncol(.), names_to = "Date", values_to = "Cases") %>% 
  mutate(Date = str_remove(Date,"X"),
         Date = as.Date(Date, "%m.%d.%y")) %>% 
  filter(Date == max(Date)) %>% 
  select(county = county,
         state,
         cases = Cases)

#write.csv(states, "state_cases.csv",row.names=FALSE)
#write.csv(counties, "county_cases.csv",row.names=FALSE)

#FIPS=====================================================================+++++

#county_fips <- read.csv("https://raw.githubusercontent.com/kjhealy/fips-codes/master/state_and_county_fips_master.csv") %>% 
#  select(id = fips,
#         name,
#         state) %>% 
#  mutate(id = str_pad(as.character(id), 5, pad = "0"),
#         name = str_remove(name, " County"),
#         name = toTitleCase(tolower(as.character(name))),
#         id = case_when(grepl("000", id) ~ str_sub(id, end=-4),
#         TRUE ~ id))
#
#write.csv(county_fips, "county_fips.csv",row.names=FALSE)

county_fips <- read.csv("county_fips.csv")

cases <- county_fips %>% filter(is.na(state)) %>% 
  left_join(states, by= c("name" = "state")) %>% 
  select(-state) %>% 
  rbind(
    county_fips %>% filter(!is.na(state)) %>% 
      left_join(counties, by= c("name" = "county","state" = "state")) %>% 
      select(-state)
  )

write.csv(cases, "cases.csv",row.names=FALSE)

库(tidyverse)
图书馆(工具)
dat%
枢轴长度(5:ncol(.),名称到=“日期”,值到=“案例”)%>%
突变(日期=str_删除(日期,“X”),
日期=截止日期(日期,“%m.%d.%y”))%>%
筛选器(日期==最大(日期))%>%
选择(州=省。州,
案例=案例)
县%
过滤器(Country.Region==“美国”,
grepl(“,”,省、州),
!grepl(“公主”,省、州),
省、州!=“华盛顿特区”)%>%
单独(省、州,c(“县”、“州”),“,”%>%
突变(县=str_删除(县,“县”))%>%
pivot_更长(6:ncol(.),name_to=“Date”,value_to=“Cases”)%>%
突变(日期=str_删除(日期,“X”),
日期=截止日期(日期,“%m.%d.%y”))%>%
筛选器(日期==最大(日期))%>%
选择(县=县,
国家,,
案例=案例)
#write.csv(表示“state_cases.csv”,row.names=FALSE)
#write.csv(countries,“country_cases.csv”,row.names=FALSE)
#菲普斯=====================================================================+++++
#县fips%
#选择(id=fips,
#名字,
#州)%>%
#变异(id=str_pad)(如字符(id),5,pad=“0”),
#name=str_remove(名称,“县”),
#name=toTitleCase(tolower(as.character(name)),
#id=case_当(grepl(“000”,id)~str_sub(id,end=-4),
#真~id)
#
#write.csv(country_fips,“country_fips.csv”,row.names=FALSE)
县fips%
左连接(状态,由=c(“名称”=“状态”))%>%
选择(-state)%%>%
阿尔宾德(
县fips%%>%过滤器(!is.na(州))%%>%
左连接(县,由=c(“名称”=“县”,“州”=“州”))%>%
选择(-state)
)
write.csv(cases,“cases.csv”,row.names=FALSE)
HTML:


冠状病毒病例
.背景{
填充:无;
指针事件:全部;
}
#州{
填充:无;
}
#美国。活跃{
显示:无;
}
#国家边界{
填充:无;
冲程:#fff;
笔画宽度:1px;
笔划线条连接:圆形;
笔划线头:圆形;
指针事件:无;
}
.县界{
填充:“灰色”;
冲程:#fff;
笔划宽度:.5px;
}
.县边界:悬停,.州:悬停{
填充:rgba(121、121、121、0.2);
}
var x=d3.scaleLinear()
.domain([1,10])
.rangeRound([600860]);
var color=d3.scaleThreshold()
.域(d3.范围(0,9))
.范围(d3.模式值[9]);
var保证金={
前10名,
底部:10,
左:10,,
右:10
},width=parseInt(d3.select('.viz')。style('width'))
,width=width-margin.left-margin.right
,映射比=0.5
,高度=宽度*mapRatio
,活动=d3。选择(空);
var svg=d3.select('.viz').append('svg'))
.attr('height',height+margin.top+margin.bottom)
.attr('width',width+margin.left+margin.right);
append('rect')
.attr('class','background center container'))
.attr('height',height+margin.top+margin.bottom)
.attr('width',width+margin.left+margin.right)
.打开(“单击”,单击);
var承诺=[
d3.json(“us-10m.v1.json”),
d3.csv(“cases.csv”,函数(d){
cases.set(d.id,+d.name+d.cases);
})
]
承诺。全部(承诺)。然后(准备好)
var projection=d3.geoAlbersUsa()
.translate([宽度/2,高度/2])
.刻度(宽度);
var path=d3.geoPath()
.投影(投影);
var g=svg.append(“g”)
.attr('class','center container center items us state')
.attr('transform','translate('+margin.left+','+margin.top+'))
.attr('width',width+margin.left+margin.right)
.attr('height',height+margin.top+margin.bottom)
功能就绪(美国){
g、 附加(“g”)
.attr(“id”、“县”)
.selectAll(“路径”)
.data(topojson.feature(us,us.objects.countries).features)
.enter().append(“路径”)
.attr(“d”,路径)
.attr(“填充”,函数(d){
var sn=CountyNames.get(d.id)
d、 速率=cases.get(countyNames.get(d.id))|0
var col=颜色(d.速率);
if(col){
返回列
}否则{
返回“#ffffff”
}
})
.attr(“类别”、“县边界”)
。打开(“单击”,重置);
g、 附加(“g”)
.attr(“id”、“状态”)
.selectAll(“路径”)
.data(topojson.feature(us,us.objects.states).features)
.enter().append(“路径”)
.attr(“d”,路径)
.attr(“填充”,函数(d){
var sn=StateNames.get(d.id)
d、 rate=cases.get(stateNames.get(d.id))|0
var col=颜色(d.速率);
if(col){
返回列
}否则{
返回“#ffffff”
}
})
.attr(“类别”、“状态”)
。开启(“点击”,点击);
g、 附加(“路径”)
.基准面(地形)