【发布时间】:2016-01-14 17:41:56
【问题描述】:
使用 ggplot2 创建了一个choropleth。这是ggplot代码
okc <- ggplot() +
geom_polygon(data = mapdata, aes(x = long, y = lat, group = group,
fill = B19013_001), color = "black", size = 0.5)+
scale_fill_distiller(palette = "Reds", labels = comma,
breaks = pretty_breaks(n = 10), values = c(1,0)) +
guides(fill = guide_legend(reverse = TRUE)) +
theme_nothing(legend = TRUE) +
ggtitle('Map of 40109')
这是来自 mapdata 的数据示例:
long lat order hole piece group id
1 -97.54285 35.51951 1 FALSE 1 40109100100.1 40109100100
2 -97.54282 35.51954 2 FALSE 1 40109100100.1 40109100100
3 -97.54280 35.51963 3 FALSE 1 40109100100.1 40109100100
4 -97.54276 35.51976 4 FALSE 1 40109100100.1 40109100100
5 -97.54270 35.51993 5 FALSE 1 40109100100.1 40109100100
6 -97.54266 35.52016 6 FALSE 1 40109100100.1 40109100100
NAME state county tract B19013_001
1 Census Tract 1001, Oklahoma County, Oklahoma 40 109 100100 33440
2 Census Tract 1001, Oklahoma County, Oklahoma 40 109 100100 33440
3 Census Tract 1001, Oklahoma County, Oklahoma 40 109 100100 33440
4 Census Tract 1001, Oklahoma County, Oklahoma 40 109 100100 33440
5 Census Tract 1001, Oklahoma County, Oklahoma 40 109 100100 33440
6 Census Tract 1001, Oklahoma County, Oklahoma 40 109 100100 33440
它产生了这个情节。
我还使用 ggmap 创建了道路地图。代码如下:
map <- get_map(location = c(lon = mean(mapdata$lon), lat = mean(mapdata$lat))
, zoom = 10
, maptype = "roadmap"
, color = "bw")
p <- ggmap(map) +
scale_x_continuous(limits = c(min(mapdata$lon), max(mapdata$lon)), expand = c(0, 0)) +
scale_y_continuous(limits = c(min(mapdata$lat), max(mapdata$lat)), expand = c(0, 0))
p
这是它生成的地图。
当我尝试组合它们时,虽然我得到了一个错误。这是我用来组合它们和错误的代码:
okc <- okc + p
Error in p + o : non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.gg", "Ops.data.frame") for "+"
我不确定为什么会收到此错误。是不是因为地图的比例不一样?除了使用非常不精确的缩放功能之外,我不知道如何缩放 ggmap。 如果有人对如何在 ggmap 上分层等值线有任何想法,我将不胜感激。
这是重新创建 ggplot choropleth 的其余代码。
library(acs)
library(ggplot2)
library(ggmap)
library(UScensus2010)
library(RColorBrewer)
library(dplyr)
library(scales)
#http://api.census.gov/data/key_signup.html
api.key.install(key="c369cd6ed053a84332caa62301eb8afe98bed825")
# Load in Shape File (You'll need to download this file from the census)
#ftp://ftp2.census.gov/geo/tiger/TIGER2013/TRACT/tl_2013_40_tract.zip
## load, subset shapefile
geodat<-readShapePoly("insert shapefile here", proj4string=CRS('+proj=longlat +datum=NAD83'))
geodat<-geodat[geodat$COUNTYFP==109,]
## fortify for ggplot digestion
geodat.f<-fortify(geodat,region="GEOID")
# American Community Survey Data: Median HH Income for OK Census Tracts
ok.counties=geo.make(state="OK", county="Oklahoma", tract="*")
ok.income<-acs.fetch(geography=ok.counties, table.number="B19013", endyear=2013)
# Merge Data Sets
geo_dat<-geography(ok.income)
var_dat<-as.data.frame(estimate(ok.income))
acs_data<-cbind(geo_dat,var_dat)
acs_data$id<- paste("40109", acs_data$tract, sep = "")
## from dplyr
mapdata<-left_join(geodat.f,acs_data)
okc <- ggplot() +
geom_polygon(data = mapdata, aes(x = long, y = lat, group = group,
fill = B19013_001), color = "black", size = 0.5)+
scale_fill_distiller(palette = "Reds", labels = comma,
breaks = pretty_breaks(n = 10), values = c(1,0)) +
guides(fill = guide_legend(reverse = TRUE)) +
theme_nothing(legend = TRUE) +
ggtitle('Map of OKC')
【问题讨论】:
-
根据提供的数据,我真的做不了很多。因为你想在地图上绘制多边形,你想做这样的事情:
ggmap(map) + geom_polygon(data = mapdata, aes(x = long, y = lat, group = group, fill = B19013_001), color = "black", size = 0.5)我想你想提供一个基础层,这是一张地图,使用ggmap()然后你在它上面绘制多边形. -
@jazzurro 是的,这正是我想要做的。您提供的代码是一个好的开始,但它将等值线堆叠在道路地图的顶部。有没有办法改变等值线的不透明度,这样你就可以真正看到下面的东西?
-
我明白了。在这种情况下,您想在
geom_polygon()中使用alpha。你可以做;ggmap(map) + geom_polygon(data = mapdata, aes(x = long, y = lat, group = group, fill = B19013_001), color = "black", size = 0.5, alpha = 0.5)。使用 alpha 值(介于 0 和 1 之间),看看哪个值可以为您提供正确的图像。 -
顺便说一下,您可能需要提供您使用的形状文件。只是说
"insert shapefile here"不会让 SO 用户帮助你。 -
@jazzurro 如何直接添加?我在上面插入了指向 shapefile 的链接,但尝试运行代码的人是否必须下载它并将其保存到自己的系统中?
标签: r ggplot2 ggmap choropleth