使用R中的GGplot填充地图多边形

4
为了我的学校作业,我想制作一张荷兰地图,其中填充的颜色范围取决于整数数字(居民数量)。我有一个名为mun_neth的数据集,它是一个SpatialPolygonDataFrame,包含荷兰所有多边形和我要绘制的所有数据。我尝试了三种不同的方法,我已经添加在下面。我还放置了错误消息。我认为我误解了填充要求。哪里出了问题,我该如何修复?
在stackoverflow上搜索后,我觉得我已经接近绘制地图了。但不幸的是,它还没有工作。
# Set workspace
getwd()
setwd("~/Wageningen/2.2 Geoscripting/data")

# Load libraries
install.packages("RCurl", dependencies=TRUE)
library(RCurl)
install.packages("ggplot2", dependencies=TRUE)
library(ggplot2)
install.packages("rgdal", dependencies=TRUE)
library(rgdal)
# Load in data
dl_from_dropbox <- function(x, key) {
  require(RCurl)
  bin <- getBinaryURL(paste0("https://dl.dropboxusercontent.com/s/", key, "/", x),           
                      ssl.verifypeer = FALSE)
  con <- file(x, open = "wb")
  writeBin(bin, con)
  close(con)
  message(noquote(paste(x, "read into", getwd())))                        
}

dl_from_dropbox("Netherlands.zip", "bocfjn1l2yhxzxe")
unzip("Netherlands.zip")
mun_neth <- readOGR('gem_2012_v1.shp', layer = 'gem_2012_v1')

# First attempt
mun_neth <- readOGR('gem_2012_v1.shp', layer = 'gem_2012_v1')
mun_neth@data$id <- rownames( mun_neth@data )
mun_neth.df <- as.data.frame( mun_neth )
mun_neth.fort <- fortify( mun_neth , region = "id" )
mun_neth.gg <- join( mun_neth.fort , mun_neth.df , by = "id" )

ggplot(data = mun_neth, aes(long, lat, group=group)) +
  geom_map(aes(fill = mun_neth$AANT_INW, color = category), map =mun_neth.gg) +
  scale_fill_gradient(high = "red", low = "white", guide = "colorbar")
  scale_colour_hue(h = c(120, 240))

每个多边形都定义了区域

错误:无法分配大小为9.5 Mb的向量

# second attempt
ggplot(mun_neth$AANT_INW, aes(x=T_MEAN))

错误:ggplot2不知道如何处理整数类数据

# Third attempt
ggplot(aes(x=x,y=y,fill=AANT_INW),data=mun_neth)

每个多边形都定义了所属区域

错误:图中没有图层


感谢您发布代码,但查看您的数据会有所帮助,特别是您使用的shapefile链接。这可能是最初的公共数据... - SlowLearner
你现在可以从我的Dropbox中加载它并运行代码。而且你是对的,这是公共数据。 - Zuenie
@Zuenie,你知道荷兰地图还能在哪里找到吗?先谢谢了! - rdatasculptor
这就是它!该网站是荷兰语的,但超链接会带您到那里;):http://www.cbs.nl/nl-NL/menu/themas/dossiers/nederland-regionaal/publicaties/geografische-data/archief/2013/2013-2012-b68-pub.htm - Zuenie
1个回答

10
假设已经下载了 shapefile 文件,你可以尝试以下代码。它可能需要在外观上进行一些整理,但作为第一个近似值看起来还不错。

假设已经下载了 shapefile 文件,你可以尝试以下代码。它可能需要在外观上进行一些整理,但作为第一个近似值看起来还不错。

library(rgdal)
library(ggplot2)

work.dir <- "your_work_dir"
mun.neth <- readOGR(work.dir, layer = 'gem_2012_v1')

mun.neth.fort <- fortify(mun.neth, region = "AANT_INW")
mun.neth.fort$id <- as.numeric(mun.neth.fort$id)
mun.neth.fort$id <- mun.neth.fort$id/1000 # optionally change to thousands?
mun.neth.fort[mun.neth.fort$id <= 0, 'id'] <- NA # some areas not numerically valid,
                                                 # presumably water zones

ggplot(data = mun.neth.fort, aes(x = long, y = lat, fill = id, group = group)) +
    geom_polygon(colour = "black") +
    coord_equal() +
    theme()

荷兰


是的,是的,是的。完美的答案。非常感谢! - Zuenie
或者你可以放弃ggplot,使用基本图形:spplot(mun.neth,“AANT_INW”)其中AANT_INW是您想要绘制的对象在一行中完成很多工作。不过,您可能需要更改颜色方案。 - Spacedman
这看起来非常不错。我可以问一下在哪里下载这个shapefile吗?我找不到它。非常感谢! - rdatasculptor
很不幸,我已经不记得我从哪里下载了这个文件,但它应该是公开来源。你可以尝试荷兰的CBS - SlowLearner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接