I have been trying to build a Choropleth / Bubble map of UK for my own shiny dashboard project.
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.
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
If you do not already have them, install the packages sp
, rgeos
and maptools
.
Now some fairly simple R code
library(maptools)
UK_adm2 = readShapeSpatial("PathToData\GBR_adm2.shp")
plot(UK_adm2)
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.
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))
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)
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))
except that where I wrote col=rainbow(12, alpha=0.6)
you will need to substitute a color scheme that is meaningful.
- Get UK shapefiles from GADM
- Install the
cartography
package - Read the vignette section on choropleth maps
Some example data would be required to recommend more precise steps.
来源:https://stackoverflow.com/questions/41495747/how-to-create-a-choropleth-or-bubble-map-of-uk-in-r