Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用美国县级数据创建Choropleth地图_R_Ggplot2_Geospatial_Ggmap_Choropleth - Fatal编程技术网

使用美国县级数据创建Choropleth地图

使用美国县级数据创建Choropleth地图,r,ggplot2,geospatial,ggmap,choropleth,R,Ggplot2,Geospatial,Ggmap,Choropleth,我正试图用R绘制一张县级新冠病毒感染数据的choropleth地图。我是R的新手,所以 我已经用ggmap做了一些相当基本的工作来绘制空间数据,但从来没有像这样做过。通常,我只需要在地图上覆盖感兴趣的点,这样我就可以使用geom_点和它们的lat/lon。在这种情况下,我需要构建底层地图,然后填充区域,而我正在ggplot世界中努力做到这一点 我遵循了一些我发现的在线示例,以达到以下目的: library(ggplot2) library(broom) library(geojsonio) #

我正试图用R绘制一张县级新冠病毒感染数据的choropleth地图。我是R的新手,所以

我已经用ggmap做了一些相当基本的工作来绘制空间数据,但从来没有像这样做过。通常,我只需要在地图上覆盖感兴趣的点,这样我就可以使用geom_点和它们的lat/lon。在这种情况下,我需要构建底层地图,然后填充区域,而我正在ggplot世界中努力做到这一点

我遵循了一些我发现的在线示例,以达到以下目的:

library(ggplot2)
library(broom)
library(geojsonio)

#get a county level map geoJSON file
counties <- geojson_read("https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json", what = "sp")

#filter our alaska and Hawaii
lower48 <- counties[(counties@data$STATE != "02" & counties@data$STATE != "15") ,]

#turn it into a dataframe for ggmap
new_counties <- tidy(lower48)

# Plot it
print(ggplot() +
  geom_polygon(data = new_counties, aes( x = long, y = lat, group = group), fill="#69b3a2", color="white") +
  theme_void() +
  coord_map())
库(ggplot2)
图书馆(扫帚)
图书馆(geojsonio)
#获取县级地图geoJSON文件

县有很多方法可以做到这一点,但概念基本相同:找到包含国家级FIPS代码的地图,并使用它们与数据源链接,也包含相同的FIPS代码以及绘图变量(这里是每天新冠病毒-19例数)


#获取新冠病毒病例,可从以下网站获得:

url使用
sf::st_read()
读取geojson,因此您可以将其作为
sf
对象获取。然后使用
ggplot2::geom_sf()
进行绘图。-
sf
sp
的继承者,因此我建议任何人远离
sp
。如果
sf
对象已经是data.frame。因此,所有标准的过滤和子集操作都“正常工作”。这太棒了……我使用
choroplethr
包取得了一些进展,但这让我感到羞愧。我将对此进行研究…出于某种原因,它不喜欢从URL中读取的countyFIPS列名,并将I umlaut(或您称之为什么)粘贴在上面,因此我手动删除了它。你知道在地图上覆盖一些纬度/经度几何点需要什么转换吗?照现在的情况,他们都在南达科他州结束了一个警告
变换,在离散的y轴中引入了无穷大的值
。我在尝试另一个软件包时遇到了这个问题,它与地图使用的投影有关。该软件包提供了一个transform()函数,使lat/lon的绘图表…忽略…使用
usmap\u transform
软件包中的
usmap\u transform
函数解决了这个问题。我修复了关于感染日期的小错误。关于灰色县,这些县在整个期间至少有一天有病例,而白色县在任何时候都没有病例。我认为这是一个很好的“功能”,但应该有一种方法来禁用它。。。如果您不喜欢灰色,可以在“比例填充”渐变函数中添加na.values=“白色”或na.values=“透明”。
head(new_counties)
# A tibble: 6 x 7
   long   lat order hole  piece group id  
  <dbl> <dbl> <int> <lgl> <chr> <chr> <chr>
1 -85.4  33.9     1 FALSE 1     0.1   0    
2 -85.4  33.9     2 FALSE 1     0.1   0    
3 -85.4  33.9     3 FALSE 1     0.1   0    
4 -85.4  33.9     4 FALSE 1     0.1   0    
5 -85.4  33.9     5 FALSE 1     0.1   0    
6 -85.4  33.8     6 FALSE 1     0.1   0 
library(ggplot2)
library(broom)
library(geojsonio)

#get a county level map geoJSON file
counties <- geojson_read("https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json", what = "sp")

#filter our alaska and Hawaii
lower48 <- counties[(counties@data$STATE != "02" & counties@data$STATE != "15") ,]

#add my own FIPS code
lower48@data$myFIPS <- substr(as.character(lower48@data$GEO_ID),1,5)  

#turn it into a dataframe for ggmap
new_counties <- tidy(lower48, region = "myFIPS")


# Plot it
print(ggplot() +
  geom_polygon(data = new_counties, aes( x = long, y = lat, group = group), fill="#69b3a2", color="white") +
  theme_void() +
  coord_map())
#devtools::install_github("UrbanInstitute/urbnmapr")
library(urbnmapr) # For map
library(ggplot2)  # For map
library(dplyr)    # For summarizing
library(tidyr)    # For reshaping
library(stringr)  # For padding leading zeros
# Get COVID cases, available from:
url <- "https://static.usafacts.org/public/data/covid-19/covid_confirmed_usafacts.csv
             ?_ga=2.162130428.136323622.1585096338-408005114.1585096338"

COV <- read.csv(url, stringsAsFactors = FALSE)
names(COV)[1] <- "countyFIPS"  # Fix the name of first column. Why!?
Covid <- pivot_longer(COV, cols=starts_with("X"), 
                     values_to="cases",
                     names_to=c("X","date_infected"),
                     names_sep="X") %>%                
  mutate(date_infected = as.Date(date_infected, format="%m.%d.%Y"),
         countyFIPS = str_pad(as.character(countyFIPS), 5, pad="0"))

# Obtain map data for counties (to link with covid data) and states (for showing borders)
states_sf <- get_urbn_map(map = "states", sf = TRUE)
counties_sf <- get_urbn_map(map = "counties", sf = TRUE)

# Merge county map with total cases of cov
counties_cov <- inner_join(counties_sf, group_by(Covid, countyFIPS) %>%
       summarise(cases=sum(cases)), by=c("county_fips"="countyFIPS"))

counties_cov %>%
  ggplot() +
  geom_sf(mapping = aes(fill = cases), color = NA) +
  geom_sf(data = states_sf, fill = NA, color = "black", size = 0.25) +
  coord_sf(datum = NA) +   
  scale_fill_gradient(name = "Cases", trans = "log", low='pink', high='navyblue', 
                      na.value="white", breaks=c(1, max(counties_cov$cases))) +
  theme_bw() + theme(legend.position="bottom", panel.border = element_blank())
library(gganimate)

counties_cov <- inner_join(counties_sf, Covid, by=c("county_fips"="countyFIPS"))

p <- ggplot(counties_cov) + ... # as above

p <- p + transition_time(date_infected) +
  labs(title = 'Date: {frame_time}')

animate(p, end_pause=30)