如何在R中使用FIPS代码(交互式地)将县级数据映射为热图?

时间:2019-09-29 06:21:14

标签: r ggmap tmap

我希望创建一个交互式地图,使我能够创建一个绘图,用户可以在其中更改年份和绘制的变量。我已经看到使用了tmap软件包,所以我在想像这样的东西,但是我也会为静态地图或其他交互式方法提供建议。我的数据比这要丰富得多,但看起来像这样:

example <- data.frame(fips = rep(as.numeric(c("37001", "37003", "37005", "37007", "37009", "37011", "37013", "37015", "37017", "37019"), 4)),
                      year = c(rep(1990, 10), rep(1991, 10), rep(1992, 10), rep(1993, 10)),
                      life = sample(1:100, 40, replace=TRUE),
                      income = sample(8000:1000000, 40, replace=TRUE),
                      pop = sample(80000:1000000, 40, replace=TRUE))

我希望我的输出是我数据集中包含的所有县的地图(在我的情况下,我拥有北卡罗来纳州的所有县,因此我不想要整个美国的地图),将显示感兴趣的选定变量的热图(在此示例数据中,yearlifeincomepop。理想情况下,我想要一个带有两个下拉列表的图类型的菜单,可让您选择要查看的年份以及要查看的变量。如果您不知道该怎么办,我(而不是用户)在其中定义年份和变量的静态地图会很有帮助交互式的东西。

我尝试了以下操作(取自here),但它是静态的,这不是我的理想选择,而且似乎还试图绘制整个美国的地图,因此该部分实际上包含在我的数据中(北卡罗来纳州)很小。

library(maps)
library(ggmap)
library(mapproj)
data(county.fips)
colors = c("#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", 
           "#980043")

example$colorBuckets <- as.numeric(cut(example$life, c(0, 20, 40, 60, 80, 
                                                          90, 100)))
colorsmatched <- example$colorBuckets[match(county.fips$fips, example$fips)]

map("county", col = colors[colorsmatched], fill = TRUE, resolution = 0, 
    lty = 0, projection = "polyconic")

1 个答案:

答案 0 :(得分:0)

这几乎是整个解决方案。我曾希望某些软件包允许仅通过fips代码完成映射,但是还没有找到。您必须下载shapefile并通过fips代码将其合并。这段代码除了可以按年份过滤外,还可以执行我上面想要的所有操作。我已经问过这个问题here,所以希望有人能在那里回答。

# get shapefiles (download shapefiles [here][1] : http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_county_5m.zip )

usgeo <- st_read("~/cb_2014_us_county_5m/cb_2014_us_county_5m.shp") %>%
  mutate(fips = as.numeric(paste0(STATEFP, COUNTYFP)))


### alternatively, this code *should* allow you download data ### 
### directly, but somethings slightly wrong. I'd love to know what. ####
# temp <- tempfile()
# download.file("http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_county_5m.zip",temp)
# data <- st_read(unz(temp, "cb_2014_us_county_5m.shp"))
# unlink(temp)
########################################################

# create fake data
example <- data.frame(fips = rep(as.numeric(c("37001", "37003", "37005", "37007", "37009", "37011", "37013", "37015", "37017", "37019"), 4)),
                      year = c(rep(1990, 10), rep(1991, 10), rep(1992, 10), rep(1993, 10)),
                      life = sample(1:100, 40, replace=TRUE),
                      income = sample(8000:1000000, 40, replace=TRUE),
                      pop = sample(80000:1000000, 40, replace=TRUE))
# join fake data with shapefiles
example <- st_as_sf(example %>%
                       left_join(usgeo))
# drop layers (not sure why, but won't work without this)
example$geometry <- st_zm(example$geometry, drop = T, what = "ZM")
# filter for a single year (which I don't want to have to do)
example <- example %>% filter(year == 1993)
# change projection
example <- sf::st_transform(example, "+proj=longlat +datum=WGS84")


# create popups
incomepopup <- paste0("County: ", example$NAME, ", avg income = $", example$income)
poppopup <- paste0("County: ", example$NAME, ", avg pop = ", example$pop)
yearpopup <- paste0("County: ", example$NAME, ", avg year = ", example$year)
lifepopup <- paste0("County: ", example$NAME, ", avg life expectancy = ", example$life)

# create color palettes
yearPalette <- colorNumeric(palette = "Blues", domain=example$year)
lifePalette <- colorNumeric(palette = "Purples", domain=example$life)
incomePalette <- colorNumeric(palette = "Reds", domain=example$income)
popPalette <- colorNumeric(palette = "Oranges", domain=example$pop)

# create map
leaflet(example) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(stroke=FALSE,
              smoothFactor = 0.2,
              fillOpacity = .8,
              popup = poppopup,
              color = ~popPalette(example$pop),
              group = "pop"
  ) %>% 

  addPolygons(stroke=FALSE,
              smoothFactor = 0.2,
              fillOpacity = .8,
              popup = yearpopup,
              color = ~yearPalette(example$year),
              group = "year"
  ) %>%

  addPolygons(stroke=FALSE,
              smoothFactor = 0.2,
              fillOpacity = .8,
              popup = lifepopup,
              color = ~lifePalette(example$life),
              group = "life"
  ) %>%

  addPolygons(stroke=FALSE,
              smoothFactor = 0.2,
              fillOpacity = .8,
              popup = incomepopup,
              color = ~incomePalette(example$income),
              group = "income"
  ) %>%

  addLayersControl(
    baseGroups=c("income", "year", "life", "pop"),
    position = "bottomleft",
    options = layersControlOptions(collapsed = FALSE)
  )

我仍在寻找一种添加“年份”过滤器的方法,该过滤器将是另一个交互式单选按钮框,用于按不同年份过滤数据。