如何在R中创建英国的Choropleth或气泡图 [英] How to Create a Choropleth or Bubble Map of UK in R

查看:177
本文介绍了如何在R中创建英国的Choropleth或气泡图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为自己的闪亮仪表板项目构建英国的Choropleth/Bubble地图.

I have been trying to build a Choropleth / Bubble map of UK for my own shiny dashboard project.

我的仪表板是一个学生信息管理系统,该地图应该能够显示学生来自哪个地区(邮政编码或城市也可以).不幸的是,大多数软件包(例如plotly和ggmap)仅包含有关美国各州的信息.

My dashboard is a student information management system and the map is supposed to show students come from which regions (zip codes or cities is also fine). Unfortunately, most packages, such as plotly and ggmap, only cover information about states of USA.

那么,有人可以告诉我如何为英国创建地图吗?这是我要完成的工作的示例:英国钟楼图

So, could someone please tell me how to create a map for UK? Here is an example of what I am trying to accomplish: Choropleth Map for UK

推荐答案

这是我的评论所指的逐步实现.我没有使用我提到的外部帖子,但它看起来像是另一个参考就像我做地图一样.

This is a step-by-step implementation of what I meant by my comment. I did not use the External Post that I mentioned, but it looks like another reference that does something like what I do to make a map.

就像@FR.他说,GADM是获取所需地图数据-shapefile的好地方.

Like @FR. said in his answer, GADM is a good place to get the needed map data - a shapefile.

Go to [GADM](http://www.gadm.org/) and click on the "Download" tab.
Use the pulldown menu to select United Kingdom as the country. 
Change the file format to Shapefile.
Click OK
Click Download
You should get a zip file called GBR_adm_shp.zip
Unzip it someplace useful
There should be a bunch of files with extensions cpg, csv, dbf, prj, shp, and shx

如果尚未安装它们,请安装软件包sprgeosmaptools.

If you do not already have them, install the packages sp, rgeos and maptools.

现在有一些相当简单的R代码

Now some fairly simple R code

library(maptools)
UK_adm2 = readShapeSpatial("PathToData\GBR_adm2.shp")
plot(UK_adm2)

zip中实际上有三个shapefile:adm0,adm1和adm2.尝试全部三个以查看哪个具有所需的详细程度.另外,请注意,有一堆名称基本相同但扩展名不同的文件.您要加载.shp文件.

There are actually three shapefiles in the zip: adm0, adm1, and adm2. Try all three to see which has the level of detail that you want. Also, note that there are a bunch of files with essentially the same names, but with different extensions. You want to load the .shp file.

您可以尝试使用提供的三个shapefile,adm0,adm1和adm2(它们具有不同的详细程度).您还可以尝试裁剪一些遥远的偏远岛屿,以了解主要岛屿的情况.

You might experiment with the three shapefiles provided adm0, adm1 and adm2 (they have different levels of detail). You might also experiment with cropping off some of the distant outlying islands to get a good view of the main island.

plot(UK_adm2, xlim=c(-8,0), ylim=c(49,61))

现在您将数据保存在R对象中,您可以了解其中的内容. names(UK_adm2)将显示一些内部数据.您可以通过键入UK_adm2$NAME_2来获得县列表.让我们重点介绍贝德福德郡. plot(UK_adm2[3,], col="#FF444488", add=TRUE)

Now you have the data in an R object, you can get at what is inside. names(UK_adm2) will show you some internal data. You can get a list of the counties by typing UK_adm2$NAME_2. Let's highlight Bedfordshire. plot(UK_adm2[3,], col="#FF444488", add=TRUE)

您刚刚看到可以为各个县上色.您要做的就是弄清楚如何给它们上色.这意味着将要显示的任何数量映射到配色方案.看一下colorRampPalette.然后,您将需要这样的东西:

You just saw that you can color individual counties. What you have to do is figure out how you want to color each of them. That will mean mapping whatever quantity you want to display to a color scheme. Take a look at colorRampPalette. Then you will want something like this:

plot(UK_adm2, col=rainbow(12, alpha=0.6), xlim=c(-8,0), ylim=c(49,61))

除了我写col=rainbow(12, alpha=0.6)的地方,您将需要替换有意义的配色方案.

except that where I wrote col=rainbow(12, alpha=0.6) you will need to substitute a color scheme that is meaningful.

这篇关于如何在R中创建英国的Choropleth或气泡图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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