是否可以在没有状态坐标的情况下创建choropleth传单地图?

时间:2019-02-28 16:31:02

标签: r leaflet choropleth

我想知道,如果您没有latlong数据,但是全州名或阿拉巴马州的'AL'之类的短名称,是否可以用传单创建一个Chorolpleth地图等等。我使用tbl_df或dataframe作为数据集。例如:

 A tibble: 50 x 5
   state       Murder Assault UrbanPop  Rape
   <chr>        <dbl>   <int>    <int> <dbl>
 1 Alabama       13.2     236       58  21.2
 2 Alaska        10       263       48  44.5
 3 Arizona        8.1     294       80  31  
 4 Arkansas       8.8     190       50  19.5
 5 California     9       276       91  40.6
 6 Colorado       7.9     204       78  38.7
 7 Connecticut    3.3     110       77  11.1
 8 Delaware       5.9     238       72  15.8
 9 Florida       15.4     335       80  31.9
10 Georgia       17.4     211       60  25.8


data("USArrests", package = "datasets")
USArrests <- add_rownames(USArrests, "state")
m <- leaflet(USArrests$state) %>%
  addProviderTiles("MapBox", options = providerTileOptions(
    id = "mapbox.light",
    accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN')))

m %>% addPolygons()

1 个答案:

答案 0 :(得分:1)

您需要状态多边形的源。 tigris软件包将为您提供帮助。

一旦有了states空间数据框,便将其转换为简单的特征对象,因为我们可以将其更像传统数据框一样对待。现在可以left_join到我们的USArrests数据集。

library(tidyverse)
library(sf)
library(leaflet)
library(tigris)
options(tigris_use_cache = TRUE)

data("USArrests", package = "datasets")
USArrests <- USArrests %>% as_tibble(rownames = "state")
states_sf <- tigris::states() %>% 
  as("sf") %>% 
  rename(state=NAME) %>% 
  left_join(USArrests) %>% 
  na.omit()
#> Joining, by = "state"

states_sf
#> Simple feature collection with 50 features and 18 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -179.2311 ymin: 18.86546 xmax: 179.8597 ymax: 71.44106
#> epsg (SRID):    4269
#> proj4string:    +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
#> First 10 features:
#>    REGION DIVISION STATEFP  STATENS GEOID STUSPS          state LSAD MTFCC
#> 1       3        5      54 01779805    54     WV  West Virginia   00 G4000
#> 2       3        5      12 00294478    12     FL        Florida   00 G4000
#> 3       2        3      17 01779784    17     IL       Illinois   00 G4000
#> 4       2        4      27 00662849    27     MN      Minnesota   00 G4000
#> 5       3        5      24 01714934    24     MD       Maryland   00 G4000
#> 6       1        1      44 01219835    44     RI   Rhode Island   00 G4000
#> 7       4        8      16 01779783    16     ID          Idaho   00 G4000
#> 8       1        1      33 01779794    33     NH  New Hampshire   00 G4000
#> 9       3        5      37 01027616    37     NC North Carolina   00 G4000
#> 10      1        1      50 01779802    50     VT        Vermont   00 G4000
#>    FUNCSTAT        ALAND      AWATER    INTPTLAT     INTPTLON Murder
#> 1         A  62265597146   489902816 +38.6472854 -080.6183274    5.7
#> 2         A 138924199212 31386038155 +28.4574302 -082.4091478   15.4
#> 3         A 143788697679  6206693598 +40.1028754 -089.1526108   10.4
#> 4         A 206232257655 18929176411 +46.3158148 -094.1996628    2.7
#> 5         A  25147754905  6983312282 +38.9466584 -076.6744939   11.3
#> 6         A   2677898725  1323551636 +41.5974187 -071.5272723    3.4
#> 7         A 214042908012  2398669593 +44.3484222 -114.5588538    2.6
#> 8         A  23187396994  1028678842 +43.6726907 -071.5843145    2.1
#> 9         A 125921301190 13470062955 +35.5397100 -079.1308636   13.0
#> 10        A  23873467535  1031124865 +44.0604795 -072.6733274    2.2
#>    Assault UrbanPop Rape                       geometry
#> 1       81       39  9.3 MULTIPOLYGON (((-81.74725 3...
#> 2      335       80 31.9 MULTIPOLYGON (((-82.98624 2...
#> 3      249       83 24.0 MULTIPOLYGON (((-91.18529 4...
#> 4       72       66 14.9 MULTIPOLYGON (((-96.78438 4...
#> 5      300       67 27.8 MULTIPOLYGON (((-77.45881 3...
#> 6      174       87  8.3 MULTIPOLYGON (((-71.67264 4...
#> 7      120       54 14.2 MULTIPOLYGON (((-116.8997 4...
#> 8       57       56  9.5 MULTIPOLYGON (((-72.3299 43...
#> 9      337       45 16.1 MULTIPOLYGON (((-82.41674 3...
#> 10      48       32 11.2 MULTIPOLYGON (((-73.31328 4...

从这里开始,我们仅遵循the tutorial here.

中的示例
pal <- colorQuantile("YlOrRd", domain = states_sf$Murder)

m <- leaflet(states_sf) %>%
  setView(-96, 37.8, 4) %>%
  addTiles()

m <- m %>% addPolygons(
  fillColor = ~pal(Murder),
  weight = 2,
  opacity = 1,
  color = "white",
  dashArray = "3",
  fillOpacity = 0.7)
#> Warning: sf layer has inconsistent datum (+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs).
#> Need '+proj=longlat +datum=WGS84'
m

enter image description here