使用ggplot从SpatialPolygonsDataFrame创建一个choropleth [英] Using ggplot to create a choropleth from a SpatialPolygonsDataFrame
问题描述
我有一个来自UScensus2010包的SpatialPolygonsDataFrame。我试图创造一个choropleth。当我这样做时,它会按预期工作:
data(colorado.county10)
choropleth(colorado.county10,
P0010001,
color = list(fun =rainbow,
attr = list(4)),
main =2010美国县,
type =plot,
border =black)
但由于'p0010001'未被发现
data(colorado.county10)
ggplot(colorado.county10,aes(long, lat = group = group))+
geom_polygon(aes(fill = P0010001),color = alpha(white,1/2),size = 0.2)+
scale_fill_brewer(pal =PuRd )
正如我试图弄清楚这一点,我注意到colorado.county10 $ P0010001返回一个数组数组,但是colorado.county10 [,P0010001]返回一个SpatialPolygonsDataFrame。
对发生了什么的深入了解
如果y你需要使用 ggplot
,你需要从 SpatialPolygonsDataFrame
强制转换为 data.frame
。
ggplot2
提供了一些 fortify
方法来创建正确格式化的数据。
目前, fortify.SpatialPolygonsDataFrame
方法不会保留 data
组件,它提供了一个列 id
,其中包含数据中的rownames。在原始
。注意, SpatialPolygonsDataFrame
的 data
插槽内的 data.frames
是一种低效的方式来存储这些信息(每个多边形的每个顶点有1行)。
因此,以下内容可以正常工作,但速度很慢,可能会导致内存问题。
c10 < - fortify(colorado.county10)
c10d < - cbind(c10,colorado.county10@data [c10 $ id,])
ggplot(c10d,aes(long,lat,group = group))+
geom_polygon(aes(fill = factor(P0010001)),color = alpha(white,1/2),size = 0.2) +
scale_fill_brewer(pal =PuRd)
使用 base
绘图功能将更快,并且不会咀嚼尽可能多的资源。
I have a SpatialPolygonsDataFrame that comes from the UScensus2010 package. I am trying to create a choropleth. When I do so, this works as expected:
data(colorado.county10)
choropleth(colorado.county10,
"P0010001",
color = list(fun = "rainbow",
attr = list(4)),
main="2010 US Counties",
type="plot",
border="black")
but this fails due to 'P0010001' not being found
data(colorado.county10)
ggplot(colorado.county10, aes(long, lat, group = group)) +
geom_polygon(aes(fill = P0010001), colour = alpha("white", 1/2), size = 0.2) +
scale_fill_brewer(pal = "PuRd")
As I've tried to figure this out, I've noted that colorado.county10$P0010001 returns an array of numbers, but colorado.county10[,"P0010001"] returns a SpatialPolygonsDataFrame.
Any insights into what is happening?
If you want to use ggplot
you need to coerce from a SpatialPolygonsDataFrame
to a data.frame
.
ggplot2
provides a number of fortify
methods that will create the correctly formatted data.
Currently the fortify.SpatialPolygonsDataFrame
method does not retain the data
component, it does provide a column id
which contains the rownames from the data.frame
within the data
slot of the original SpatialPolygonsDataFrame
.
Note that data.frames
are an inefficient way to store this information (1 row for each vertex for each polygon).
Thus the following will work, but is slow and may cause memory issues
c10 <- fortify(colorado.county10)
c10d <- cbind(c10, colorado.county10@data[c10$id,])
ggplot(c10d, aes(long, lat, group = group)) +
geom_polygon(aes(fill = factor(P0010001)), colour = alpha("white", 1/2), size = 0.2) +
scale_fill_brewer(pal = "PuRd")
Using base
plotting functions will be much faster and not chew up as many resources.
这篇关于使用ggplot从SpatialPolygonsDataFrame创建一个choropleth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!