为了我的学校作业,我想制作一张荷兰地图,其中填充的颜色范围取决于整数数字(居民数量)。我有一个名为mun_neth的数据集,它是一个SpatialPolygonDataFrame,包含荷兰所有多边形和我要绘制的所有数据。我尝试了三种不同的方法,我已经添加在下面。我还放置了错误消息。我认为我误解了填充要求。哪里出了问题,我该如何修复?
在stackoverflow上搜索后,我觉得我已经接近绘制地图了。但不幸的是,它还没有工作。
在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)
每个多边形都定义了所属区域
错误:图中没有图层