区域多边形不显示在ggplot2等值线图中 [英] Region polygons not displaying in ggplot2 Choropleth map

查看:232
本文介绍了区域多边形不显示在ggplot2等值线图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用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)#为要着色的区域添加值
gadm @ data $ id = rownames(gadm @ data )#在shapefile数据中创建一个id
ghaMap< - fortify(gadm,region =id)
colnames(ghaDF [5])< - id
ghaMap< ; - 合并(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 变量进行着色。任何建议?
$ b

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

解决方案

你的问题很简单。 Change

  colnames(ghaDF [5])<  - id

  colnames(ghaDF)[5]< ;  - id

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

上面的第一行提取了 ghaDF 的第5列作为1列的数据框,将该列名设置为 id,并在调用 colnames(...)完成时放弃它。

第二行提取 ghaDF

的列名称, code>作为一个字符向量,并将该向量的第5个元素设置为id,这实际上改变了 ghaDF



说完这一切,您的工作流程有点受到折磨。一方面,您将 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 =浅绿色,高=深绿色)+
coord_map()


I'm trying to do a very basic map plot with ggplot2. I can't find why the colored polygons would not display. It seems that my code is no different from what I can find on the many tutorials and the questions already answered on this website. I think it could come from the way I prepare the data (see 100% reproducible example below).

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

On the image above (the output of m2), the regions should be colored according to the ghaMap$prod variable. Any suggestions?

(R version 3.0.2 - Platform: x86_64-w64-mingw32/x64 (64-bit))

解决方案

Your problem is very simple. Change

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

to

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

and your code produces the map you want.

The first line above extracts column 5 of ghaDF as a data frame with 1 column, sets that column name to "id", and discards it when the call to colnames(...) completes. It does not affect the original data frame at all.

The second line extracts the column names of ghaDF as a character vector, and sets the 5th element of that vector to "id", which in effect changes the name of column 5 of ghaDF.

Having said all this, your workflow is a bit tortured. For one thing you are merging all the columns of gadm@data into ghaMap, which is unnecessary and extremely inefficient. The code below produces the same map:

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()

这篇关于区域多边形不显示在ggplot2等值线图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆