我在R中有一个名为obesity_map的数据框,它基本上给出了州、县和每个县的肥胖率。它看起来或多或少像这样:
obesity_map = data.frame(state, county, obesity_rate)
我试图通过显示全美每个县的不同肥胖率,在地图上显示这一点:
us.state.map <- map_data('state')
head(us.state.map)
states <- levels(as.factor(us.state.map$region))
df <- data.frame(region = states, value = runif(length(states), min=0, max=100),stringsAsFactors = FALSE)
map.data <- merge(us.state.map, df, by='region', all=T)
map.data <- map.data[order(map.data$order),]
head(map.data)
map.county <- map_data('county')
county.obesity <- data.frame(region = obesity_map$state, subregion = obesity_map$county, value = obesity_map$obesity_rate)
map.county <- merge(county.obesity, map.county, all=TRUE)
ggplot(map.county, aes(x = long, y = lat, group=group, fill=as.factor(value))) + geom_polygon(colour = "white", size = 0.1)
它基本上创建了一个如下所示的图像:
正如你所看到的,美国被划分成奇怪的形状,颜色在不同的渐变中不是一种一致的颜色,你不能从中获得太多。但我真正想要的是下面这样的内容,但需要填写每个县:
我是个新手,所以我非常感谢大家的帮助!
编辑:
下面是dput的输出:
dput(obesity_map)
structure(list(X = 1:3141, FIPS = c(1L, 3L, 5L, 7L, 9L, 11L,
13L, 15L, 17L, 19L, 21L, 23L, 25L, 27L, 29L, 31L, 33L, 35L, 37L,
39L, 41L, 43L, 45L, 47L, 49L, 51L, 53L, 55L, 57L, 59L, 61L, 63L,
65L, 67L, 69L, 71L, 73L, 75L, 77L, 79L, 81L, 83L, 85L, 87L, 89L,
91L, 93L, 95L, 97L, 99L, 101L, 103L, 105L, 107L, 109L, 111L,
113L, 115L, 117L, 119L, 121L, 123L, 125L, 127L, 129L, 131L, 133L,
13L, 16L, 20L, 50L, 60L, 68L, 70L, 90L, 100L, 110L, 122L, 130L,
150L, 164L, 170L, 180L, 185L, 188L, 201L, 220L, 232L, 240L, 261L,
270L, 280L, 282L, 290L, 1L, 3L, 5L, 7L, 9L, 11L, 12L, 13L, 15L,
17L, 19L, 21L, 23L, 25L, 27L, 1L, 3L, 5L, 7L, 9L, 11L, 13L, 15L,
17L, 19L, 21L, 23L, 25L, 27L, 29L, 31L, 33L, 35L, 37L, 39L, 41L,
这是一个巨大的数字,因为它是针对美国每个县的,所以我缩写了结果,并放入了前几行。
基本上,数据框看起来像这样:
print(head(obesity_map))
X FIPS state_names county_names obesity
1 1 1 Alabama Autauga 24.5
2 2 3 Alabama Baldwin 23.6
3 3 5 Alabama Barbour 25.6
4 4 7 Alabama Bibb 0.0
5 5 9 Alabama Blount 24.2
6 6 11 Alabama Bullock 0.0
我也试着按照上传的示例使用ggcounty,但一直收到错误。我不完全确定我做错了什么:
library(ggcounty)
# breaks
obesity_map$obese <- cut(obesity_map$obesity,
breaks=c(0, 5, 10, 15, 20, 25, 30),
labels=c("1", "2", "3", "4",
"5", "6"),
include.lowest=TRUE)
# get the US counties map (lower 48)
us <- ggcounty.us()
# start the plot with our base map
gg <- us$g
# add a new geom with our population (choropleth)
gg <- gg + geom_map(data=obesity_map, map=us$map,
aes(map_id=FIPS, fill=obesity_map$obese),
color="white", size=0.125)
但是我总是得到一个错误:“错误:参数必须强制为非负整数”。
有什么想法吗?再次感谢您的帮助!我太感谢你了。
发布于 2015-12-24 21:56:01
也许对另一个答案来说有点晚了,但我认为仍然值得分享。
数据的读取和预处理类似于jlhoward的答案,但有一些不同:
library(tmap) # package for plotting
library(readxl) # for reading Excel
library(maptools) # for unionSpatialPolygons
# download data
download.file("http://www.ers.usda.gov/datafiles/Food_Environment_Atlas/Data_Access_and_Documentation_Downloads/Current_Version/DataDownload.xls", destfile = "DataDownload.xls", mode="wb")
df <- read_excel("DataDownload.xls", sheet = "HEALTH")
# download shape (a little less detail than in the other scripts)
f <- tempfile()
download.file("http://www2.census.gov/geo/tiger/GENZ2010/gz_2010_us_050_00_20m.zip", destfile = f)
unzip(f, exdir = ".")
US <- read_shape("gz_2010_us_050_00_20m.shp")
# leave out AK, HI, and PR (state FIPS: 02, 15, and 72)
US <- US[!(US$STATE %in% c("02","15","72")),]
# append data to shape
US$FIPS <- paste0(US$STATE, US$COUNTY)
US <- append_data(US, df, key.shp = "FIPS", key.data = "FIPS")
当正确的数据被附加到shape对象上时,可以用一行代码绘制一条线:
qtm(US, fill = "PCT_OBESE_ADULTS10")
这可以通过添加州边框、更好的投影和标题来增强:
# create shape object with state polygons
US_states <- unionSpatialPolygons(US, IDs=US$STATE)
tm_shape(US, projection="+init=epsg:2163") +
tm_polygons("PCT_OBESE_ADULTS10", border.col = "grey30", title="") +
tm_shape(US_states) +
tm_borders(lwd=2, col = "black", alpha = .5) +
tm_layout(title="2010 Adult Obesity by County, percent",
title.position = c("center", "top"),
legend.text.size=1)
发布于 2014-05-18 07:46:46
这是我在管理映射变量时可以得到的东西。将其重命名为“region”。
library(ggplot2)
library(maps)
m.usa <- map_data("county")
m.usa$id <- m.usa$subregion
m.usa <- m.usa[ ,-5]
names(m.usa)[5] <- 'region'
df <- data.frame(region = unique(m.usa$region),
obesity = rnorm(length(unique(m.usa$region)), 50, 10),
stringsAsFactors = F)
head(df)
region obesity
1 autauga 44.54833
2 baldwin 68.61470
3 barbour 52.19718
4 bibb 50.88948
5 blount 42.73134
6 bullock 59.93515
ggplot(df, aes(map_id = region)) +
geom_map(aes(fill = obesity), map = m.usa) +
expand_limits(x = m.usa$long, y = m.usa$lat) +
coord_map()
发布于 2015-09-23 03:50:27
我认为您所需要做的就是重新排序map.county变量,就像之前对map.data变量所做的那样。
....
map.county <- merge(county.obesity, map.county, all=TRUE)
## reorder the map before plotting
map.county <- map.county[order(map.data$county),]
## plot
ggplot(map.county, aes(x = long, y = lat, group=group, fill=as.factor(value))) + geom_polygon(colour = "white", size = 0.1)
https://stackoverflow.com/questions/23714052
复制相似问题