【问题标题】:Region polygons not displaying in ggplot2 Choropleth map区域多边形未显示在 ggplot2 Choropleth 地图中
【发布时间】:2015-01-02 23:08:12
【问题描述】:

我正在尝试用 ggplot2 绘制一个非常基本的地图。我找不到彩色多边形不显示的原因。似乎我的代码与我在许多教程和本网站上已经回答的问题中可以找到的代码没有什么不同。我认为这可能来自我准备数据的方式(请参阅下面的 100% 可重现示例)。

library(maptools)
library(sp)
library(ggplot2)
library(rgeos)
con <- url("http://biogeo.ucdavis.edu/data/gadm2/R/GHA_adm1.RData")
print(load(con))
close(con)

ghaDF<-as.data.frame(gadm)
ghaDF$prod <- c(12, 26, 12,22,0,11,4,5,4,4) # add values for the regions to be colored with
gadm@data$id = rownames(gadm@data) #create an id in the shapefile data
ghaMap <- fortify(gadm, region="id")
colnames(ghaDF[5])<-"id"
ghaMap <- merge(ghaMap, ghaDF)

m0 <- ggplot(data=ghaMap)
m1 <- m0 + geom_polygon(aes(x=long, y=lat, fill = prod, group=group)) 
         + scale_fill_gradient(low = "light green", high = "dark green")
m2 <- m1 + geom_path(aes(x=long, y=lat, group=group),color='gray')  
         + coord_equal()
m2

在上图(m2 的输出)上,区域应根据ghaMap$prod 变量着色。有什么建议吗?

(R 版本 3.0.2 - 平台:x86_64-w64-mingw32/x64(64 位))

【问题讨论】:

  • +1 对于一个非常可重复的问题。不必去寻找 shapefile 是不寻常的。

标签: r ggplot2 maps


【解决方案1】:

您的数据看起来不错。这是geom_map 的解决方案(以及colorbrewer 颜色:-):

devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")
gg <- ggplot()
gg <- gg + geom_map(data=ghaMap, map=ghaMap,
                    aes(map_id=id, group=group,
                        x=long, y=lat, fill=prod))
gg <- gg + scale_fill_gradient(low="#99d8c9", high="#00441b")
gg <- gg + coord_map()
gg <- gg + theme_map() + theme(legend.position="right")
gg

您可以省略 devtools…theme_map() 而不会影响解决方案。

编辑使用简化的多边形

library(maptools)
library(sp)
library(ggplot2)
library(rgeos)
library(rgdal)

# get the shapefile
download.file("http://biogeo.ucdavis.edu/data/gadm2/shp/GHA_adm.zip", "GHA_adm.zip")
unzip("GHA_adm.zip", exdir="GHA_adm")

# simplify it
setwd("GHA_adm")
system("ogr2ogr -simplify 0.001 simple1.shp GHA_adm1.shp") # simplify (500K -> 80K)
setwd("..")

# read it in
gadm <- readOGR("GHA_adm", "simple1")

# convert it
gadm_map <- fortify(gadm, region="NAME_1")

# make the values we want to fill with
# you can use "ID_1" but folks I've seen generally like using names. works either way
prod <- data.frame(id=gadm$NAME_1, value=c(12, 26, 12, 22, 0, 11, 4, 5, 4, 4), stringsAs=FALSE)

# merge the data together
gadm_map <- merge(gadm_map, prod, all.x=TRUE) # add it right into the fortified data frame

# plot it
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")
gg <- ggplot()
gg <- gg + geom_map(data=gadm_map, map=gadm_map,
                    aes(map_id=id, group=group,
                        x=long, y=lat, fill=value))
gg <- gg + scale_fill_gradient(low="#99d8c9", high="#00441b")
gg <- gg + coord_map()
gg <- gg + theme_map() + theme(legend.position="right")
gg

(与上面相同的地图,边缘细节略少)

使用lsos (via):

lsos()

##                               Type   Size Rows Columns
## gadm_map                data.frame 275256 5167       9
## gadm      SpatialPolygonsDataFrame 168296   10       9
## gg                              gg  34792    9      NA
## prod                    data.frame   2136   10       3

与 OP 中的相同:

##                               Type    Size  Rows Columns
## ghaMap                  data.frame 3689120 31735      18
## gadm      SpatialPolygonsDataFrame  589424    10      11
## gg                              gg   34792     9      NA
## ghaDF                   data.frame    5088    10      12
## con                            url     552     1      NA

【讨论】:

  • 非常感谢您的解决方案。不幸的是,我无法重现它。我刚刚更新了我的 R 版本和我所有的包。我注意到 gg 绘图输出大小约为 31Mb。当我输入memory.size() 时,我得到[1] 24.1 返回。我正在尝试查找是否已链接。
  • 我确实注意到这是一个巨大的 shapefile 空间数据框。你能链接到源 shapefile d/l 吗?我可以很快为您简化 shapefile。
  • shapefile可以在here获取。它是GHA_adm1
  • 非常感谢@hrbrmstr 为您花在这个解决方案上的时间。简化的多边形解决方案实际上对我有用。但我想这是由于您输入要填充的值的替代方式。但是知道如何简化 shapefile 是非常好的。谢谢。
【解决方案2】:

你的问题很简单。改变

colnames(ghaDF[5])<-"id"

colnames(ghaDF)[5]<-"id"

您的代码会生成您想要的地图。

上面的第一行将ghaDF 的第5 列提取为具有1 列的数据框,将该列名称设置为"id",并在对colnames(...) 的调用完成时将其丢弃。 完全不影响原始数据框。

第二行提取ghaDF 的列名作为字符向量,并将该向量的第5 个元素设置为"id",这实际上更改了ghaDF 的第5 列的名称。

说了这么多,您的工作流程有点受折磨。一方面,您将gadm@data 的所有列合并到ghaMap,这是不必要的并且效率极低。下面的代码生成相同的地图:

load(url("http://biogeo.ucdavis.edu/data/gadm2/R/GHA_adm1.RData"))
ghaMap <- fortify(gadm)
ghaDF  <- data.frame(id=rownames(gadm@data),
                     prod=c(12,26,12,22,0,11,4,5,4,4))
ghaMap <- merge(ghaMap,ghaDF)
ggplot(ghaMap, aes(x=long,y=lat,group=group))+
  geom_polygon(aes(fill=prod))+
  geom_path(color="gray")+
  scale_fill_gradient(low = "light green", high = "dark green")+
  coord_map()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多