【问题标题】:Plotting Thematic Maps in R Using Shapefiles and Data Files from DIfferent Sources使用来自不同来源的 Shapefile 和数据文件在 R 中绘制专题图
【发布时间】:2012-02-09 05:32:14
【问题描述】:

给定一个 shapefile,我如何塑造和使用数据文件,以便能够使用与 shapefile 中的形状区域对应的标识符来绘制专题地图?

#Download English Government Office Network Regions (GOR) from:
#http://www.sharegeo.ac.uk/handle/10672/50
tmp_dir = tempdir()
url_data = "http://www.sharegeo.ac.uk/download/10672/50/English%20Government%20Office%20Network%20Regions%20(GOR).zip"
zip_file = sprintf("%s/shpfile.zip", tmp_dir)
download.file(url_data, zip_file)
unzip(zip_file, exdir = tmp_dir)

library(maptools)

#Load in the data file (could this be done from the downloaded zip file directly?
gor=readShapeSpatial(sprintf('%s/Regions.shp', tmp_dir))

#I can plot the shapefile okay...
plot(gor)

#and I can use these commands to get a feel for the data...
summary(gor)
attributes(gor@data)
gor@data$NAME
#[1] North East               North West              
#[3] Greater London Authority West Midlands           
#[5] Yorkshire and The Humber South West              
#[7] East Midlands            South East              
#[9] East of England         
#9 Levels: East Midlands East of England ... Yorkshire and The Humber

#download data from http://www.justice.gov.uk/downloads/publications/statistics-and-data/courts-and-sentencing/csq-q3-2011-insolvency-tables.csv
#insolvency<- read.csv("~/Downloads/csq-q3-2011-insolvency-tables.csv")
insolvency=read.csv("http://www.justice.gov.uk/downloads/publications/statistics-and-data/courts-and-sentencing/csq-q3-2011-insolvency-tables.csv")
insolvencygor.2011Q3=subset(insolvency,Time.Period=='2011 Q3' & Geography.Type=='Government office region')
#tidy the data
require(gdata)
insolvencygor.2011Q3=drop.levels(insolvencygor.2011Q3)

names(insolvencygor.2011Q3)
#[1] "Time.Period"                 "Geography"                  
#[3] "Geography.Type"              "Company.Winding.up.Petition"
#[5] "Creditors.Petition"          "Debtors.Petition"  

levels(insolvencygor.2011Q3$Geography)
#[1] "East"                     "East Midlands"           
#[3] "London"                   "North East"              
#[5] "North West"               "South East"              
#[7] "South West"               "Wales"                   
#[9] "West Midlands"            "Yorkshire and the Humber"

#So what next?   

到目前为止,我该如何采取下一步来生成主题/等值线图,例如根据 Debtors.Petition 值对每个区域进行着色?

(我还刚刚注意到一个可能的问题 - 大写 GOR 级别不匹配:“Yorkshire and the Humber”和“Yorkshire and The Humber”)

【问题讨论】:

  • 似乎this SO post(右侧栏中“相关”列表中的第一个)可能会帮助您完成剩下的工作。
  • 我看到了,但是前几次都错过了修复...所以我需要做的是:gor@data=merge(insolvencygor.2011Q3,gor@data,by .x='Geography',by.y='NAME') plot(gor,col=levels(gor@data$Creditors.Petition)) 虽然具有适当的颜色映射和适当的区域名称映射(我注意到它不仅仅是约克和亨伯赛德的不匹配......)
  • 您能否将您的数据集简化为一个小示例来说明您的问题?您可以使用 save 命令保存这些并将它们上传到 SO 或服务器并在此处发布链接。这将使您更轻松地减少大量代码。
  • 我不知道如何使 shapefile 变小...我从以下位置开始:a)想要生成专题地图,b)知道存在用于在地图上生成轮廓的 shapefile,和 c) 错误,就是这样......也许值得一提的是,我在这种情况下使用 R 的原因是因为在我看来 R 可以处理 shapefile......
  • 还有其他工具可以很容易地处理 shapefile,例如QGis 或 GVSig,它们都是开源 GIS 程序。

标签: r shapefile maptools


【解决方案1】:

没有看到树木的树木,回答我自己的问题,这是一种方法(代码遵循问题中的代码):

#Convert factors to numeric [ http://stackoverflow.com/questions/4798343/convert-factor-to-integer ]
#There's probably a much better formulaic way of doing this/automating this?
insolvencygor.2011Q3$Creditors.Petition=as.numeric(levels(insolvencygor.2011Q3$Creditors.Petition))[insolvencygor.2011Q3$Creditors.Petition]
insolvencygor.2011Q3$Company.Winding.up.Petition=as.numeric(levels(insolvencygor.2011Q3$Company.Winding.up.Petition))[insolvencygor.2011Q3$Company.Winding.up.Petition]
insolvencygor.2011Q3$Debtors.Petition=as.numeric(levels(insolvencygor.2011Q3$Debtors.Petition))[insolvencygor.2011Q3$Debtors.Petition]

#Tweak the levels so they match exactly (really should do this via a lookup table of some sort?)
i2=insolvencygor.2011Q3
i2c=c('East of England','East Midlands','Greater London Authority','North East','North West','South East','South West','Wales','West Midlands','Yorkshire and The Humber')
i2$Geography=factor(i2$Geography,labels=i2c)

#Merge the data with the shapefile
gor@data=merge(gor@data,i2,by.x='NAME',by.y='Geography')

#Plot the data using a greyscale
plot(gor,col=gray(gor@data$Creditors.Petition/max(gor@data$Creditors.Petition)))

所以这种方法的作用是将数值数据合并到 shapefile 中,然后直接绘制出来。

也就是说,将数据文件和 shapefile 分开不是更简洁的方法吗? (我仍然不确定该怎么做?)

【讨论】:

  • 我不认为它会更干净,你需要结合“正常”和空间数据来制作情节。
  • 我同意你的感觉,代码可以更短,更重要,但如果没有可重现的示例,很难演示。
  • 在什么意义上可重现?将上述答案代码附加到问题中的代码会生成专题图,但诚然,下载步骤需要手动干预,而不是由脚本处理。
  • 我的意思是不可重现,因为我无法将它粘贴到 R 中并得到你所拥有的。试着让人们尽可能轻松地参与进来。
  • 好的,点了 - 但正如问题中的 cmets 所建议的那样,我不知道如何从 shapegeo 网站上捆绑的 zip 文件中加载 shape 文件?;- )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 2020-11-05
  • 1970-01-01
相关资源
最近更新 更多