本文介绍了在R中使用GGplot填充地图的多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 对于我的学校作业,我试图制作一幅荷兰地图,其中填充了一个依赖整数(公民数量)的色标。我有一个名为mun_neth的数据集,它是一个SpatialPolygonDataFrame,它包含荷兰的所有多边形以及我想绘制的所有数据。我尝试了三种不同的方法,我在下面添加了这些方法。我也把下面的错误信息。我想我误解了填充要求。在哪里出问题了,我该如何解决它? 在这里搜索完stackoverflow后,我觉得我已经变得非常接近绘制地图了。但不幸的是它还没有工作。 #设置工作区 getwd() setwd(〜/ Wageningen / 2.2 Geoscripting / data ) $ b $加载库 install.packages(RCurl,dependencies = TRUE) library(RCurl) install.packages(ggplot2,dependencies = TRUE) library(ggplot2) install.packages(rgdal,dependencies = TRUE) library(rgdal)#在数据中加载 dl_from_dropbox< - 函数(x,key){ require(RCurl) bin ssl.verifypeer = FALSE) con writeBin(bin,con) close(con)消息(noquote(paste(x,read into)) $ b (Netherlands.zip) mun_neth< - readOGR('gem_2012_v1.shp',layer ='gem_2012_v1') #第一次尝试 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< - 加入(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高=红,低=白,指南=彩条) scale_colour_hue(h = c(120,240)) 为每个Polygons定义的区域 错误:无法分配大小为9.5 Mb的矢量 #第二次尝试 ggplot(mun_neth $ AANT_INW,aes(x = T_MEAN)) 错误:ggplot2不知道如何处理类整数的数据 #第三次尝试 ggplot(aes(x = x,y = y,fill = AANT_INW),data = mun_neth) 区域s为每个Polygons定义 错误:图层中没有图层 解决方案假设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#可选择更改为数千? mun.neth.fort [mun.neth.fort $ id< = 0,'id']< - NA#一些区域在数字上不是有效的,#可能是水区 ggplot(data = mun.neth.fort,aes(x = long,y = lat,fill = id,group = group))+ geom_polygon(color =black)+ coord_equal()+ theme() For my school assignment I am trying to make a map of the Netherlands filled with a colour scale that is dependent of integer numbers (amount of citizens). I have a dataset called mun_neth which is a SpatialPolygonDataFrame and contains all polygons of the Netherlands and all the data that I want to plot. I have tried three different methods which I have added underneath. I also put the error messages underneath. I think I misunderstand the fill requirement. Where is it going wrong and how should I fix it?After searching here on stackoverflow I feel that I have become pretty close to plotting the map. But unfortunately it is not working yet. # Set workspacegetwd()setwd("~/Wageningen/2.2 Geoscripting/data")# Load librariesinstall.packages("RCurl", dependencies=TRUE)library(RCurl)install.packages("ggplot2", dependencies=TRUE)library(ggplot2)install.packages("rgdal", dependencies=TRUE)library(rgdal)# Load in datadl_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 attemptmun_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))Regions defined for each PolygonsError: cannot allocate vector of size 9.5 Mb# second attemptggplot(mun_neth$AANT_INW, aes(x=T_MEAN))Error: ggplot2 doesn't know how to deal with data of class integer# Third attemptggplot(aes(x=x,y=y,fill=AANT_INW),data=mun_neth)Regions defined for each PolygonsError: No layers in plot 解决方案 Assuming the shapefile is already downloaded, you could do something like the below. It probably needs a bit of tidying up in a cosmetic sense, but as a first approximation it seems okay.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 zonesggplot(data = mun.neth.fort, aes(x = long, y = lat, fill = id, group = group)) + geom_polygon(colour = "black") + coord_equal() + theme() 这篇关于在R中使用GGplot填充地图的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-13 19:25