用县级映射

时间:2014-04-16 17:13:35

标签: r map

我需要在R中的图片上创建一个类似的地图,但是按照县级别。我有CSV文件,其中包含佛罗里达县的名称和按县分列的人口列。 我该如何创建地图?

谢谢!

3 个答案:

答案 0 :(得分:1)

内置的maps数据有点过时了。我不知道县界是否有变化。我更喜欢这里的县地图:http://www.baruch.cuny.edu/geoportal/data/esri/esri_usa.htm

而且,这个http://www.baruch.cuny.edu/geoportal/data/esri/usa/census/counties.zip是美国县shapefile的直接链接。

如果你抓住ogr,你可以用:

提取佛罗里达州
ogr2ogr -f "ESRI Shapefile" -where "STATE_NAME = 'FLORIDA'" fl.shp counties.shp

然后将其读入R并享受一些乐趣。我找到了一些人口数据:

pop <- structure(list(County = c("Alachua", "Baker", "Bay", "Bradford", 
                                 "Brevard", "Broward", "Calhoun", "Charlotte", "Citrus", "Clay", 
                                 "Collier", "Columbia", "DeSoto", "Dixie", "Duval", "Escambia", 
                                 "Flagler", "Franklin", "Gadsden", "Gilchrist", "Glades", "Gulf", 
                                 "Hamilton", "Hardee", "Hendry", "Hernando", "Highlands", "Hillsborough", 
                                 "Holmes", "Indian River", "Jackson", "Jefferson", "Lafayette", 
                                 "Lake", "Lee", "Leon", "Levy", "Liberty", "Madison", "Manatee", 
                                 "Marion", "Martin", "Miami-Dade", "Monroe", "Nassau", "Okaloosa", 
                                 "Okeechobee", "Orange", "Osceola", "Palm Beach", "Pasco", "Pinellas", 
                                 "Polk", "Putnam", "St. Johns", "St. Lucie", "Santa Rosa", "Sarasota", 
                                 "Seminole", "Sumter", "Suwannee", "Taylor", "Union", "Volusia", 
                                 "Wakulla", "Walton", "Washington"), 
                      pop = c(248002L, 26881L, 
                              169866L, 27217L, 548424L, 1784715L, 14621L, 163679L, 140519L, 
                              192843L, 333663L, 67489L, 34367L, 16263L, 876075L, 301120L, 97843L, 
                              11562L, 47588L, 16880L, 12658L, 16106L, 14507L, 27682L, 37808L, 
                              173808L, 99092L, 1276410L, 20022L, 139586L, 50166L, 14554L, 8618L, 
                              303317L, 643367L, 278377L, 40304L, 8483L, 19395L, 333880L, 335008L, 
                              148077L, 2582375L, 73560L, 74661L, 188349L, 39762L, 1202978L, 
                              288361L, 1345652L, 473566L, 926610L, 613950L, 72605L, 201541L, 
                              281151L, 157317L, 385292L, 431074L, 105104L, 43873L, 23018L, 
                              15483L, 498978L, 30869L, 57779L, 24793L)), 
                 .Names = c("County", 
                            "pop"), class = "data.frame", row.names = c(NA, -67L))

这很容易融入一个理论:

library(sp)
library(maptools)
library(ggplot2)
library(plyr)
library(ggplot2)

# read in the florida county shapefile
fl <- readShapePoly("fl.shp", repair=TRUE, IDvar="NAME")

# make it work nicely with ggplot
fl.f <- fortify(fl, region="NAME")

# start the plot
gg <- ggplot(pop)

# plot the base mape
gg <- gg + geom_map(data=fl.f, map = fl.f, aes(map_id=id, x = long, y = lat), 
                    fill="white", color="#7f7f7f", size=0.25)

# add the county population data
gg <- gg + geom_map(map = fl.f, aes(map_id = County, fill = pop), size=0.25)

# should prbly not do this - use `cut` for explicit groupings
# i was pressed for time
gg <- gg + scale_fill_gradient(low="#fff7bc", high="#cc4c02", name="Population")
gg <- gg + theme_bw()
gg <- gg + labs(x="", y="")
gg <- gg + theme(plot.background = element_rect(fill = "transparent",colour = NA),
                 panel.border = element_blank(),
                 panel.background =element_rect(fill = "transparent",colour = NA),
                 panel.grid = element_blank(),
                 axis.text = element_blank(),
                 axis.ticks = element_blank(),
                 legend.position="right")
gg

enter image description here

请注意,您必须确保县名与fl.shp结构中的名称相符:

unique(fl.f$id)
##  [1] "Alachua"      "Baker"        "Bay"          "Bradford"     "Brevard"      "Broward"      "Calhoun"     
##  [8] "Charlotte"    "Citrus"       "Clay"         "Collier"      "Columbia"     "DeSoto"       "Dixie"       
## [15] "Duval"        "Escambia"     "Flagler"      "Franklin"     "Gadsden"      "Gilchrist"    "Glades"      
## [22] "Gulf"         "Hamilton"     "Hardee"       "Hendry"       "Hernando"     "Highlands"    "Hillsborough"
## [29] "Holmes"       "Indian River" "Jackson"      "Jefferson"    "Lafayette"    "Lake"         "Lee"         
## [36] "Leon"         "Levy"         "Liberty"      "Madison"      "Manatee"      "Marion"       "Martin"      
## [43] "Miami-Dade"   "Monroe"       "Nassau"       "Okaloosa"     "Okeechobee"   "Orange"       "Osceola"     
## [50] "Palm Beach"   "Pasco"        "Pinellas"     "Polk"         "Putnam"       "Santa Rosa"   "Sarasota"    
## [57] "Seminole"     "St. Johns"    "St. Lucie"    "Sumter"       "Suwannee"     "Taylor"       "Union"       
## [64] "Volusia"      "Wakulla"      "Walton"       "Washington"  

我没有费心去检查我的例子(看起来好像没问题),但对于真正的数据你应该这么做。

答案 1 :(得分:0)

以下是如何获得佛罗里达县的地图:

library(maps)
m <- map("county", "Florida") 

将信息添加到地图(例如热指数)是一个不同的问题。

答案 2 :(得分:0)

如果您想在县级绘制佛罗里达的人口图,请查看此https://cran.r-project.org/web/packages/usmap/vignettes/mapping.html。他们使用usmaps。