The following document describes the use of the R statistical package to generate map rendering of statistics. This document was developed as an interactive notebook using R Studio Desktop1 edition as the development and test environment. This allows the inclusion of the R2 source code blocks for each example provided. Both R and RStudio must be installed to experiment with the code. These software are free and fully supported by a vibrant user community. The urls for the installers for these software can be found in the Bibliography. It is important to choose the installer that corresponds to the operating system you are using. (The basic, free versions are all you need.)
There are numerous ways to plot information on maps with R. The 3 packages described in this section represent the most common methods used. Each of them use latitude and longitude making it possible to combine the different techniques together as shown in the next chapter. It should be noted that each technology will require the installation and load of specific packages. The code for this is given in the following sections.
This mapping technology works from an outline of the world map and requires a specific package.
You can install the package using the INSTALL button in the Packages in RStudio or copy and run the following code to download the package from the Internet. This copy only needs to be run once.
install.packages("maps")
install.packages("mapdata")
This package is not part of the core of R and must be loaded into the working library once before using its functions. The code to do this is given below:
library(maps)
library(mapdata)
Maps are drawn merely by listing the names of the countries (or region) of interest as shown by these examples:
map('worldHires')
map('worldHires',
region=c("myanmar","thailand",
"laos","cambodia","vietnam",
"malaysia","singapore",
"indonesia","brunei",
"philippines"))
map('worldHires','myanmar')
It is also possible to fill each country with a different color.
countries = c("myanmar","thailand",
"laos","cambodia","vietnam",
"malaysia","singapore",
"indonesia", "brunei",
"philippines")
len = length(countries)
colors = rainbow(len + 1)
map('worldHires',
region=countries,
fill=TRUE,col=colors[1])
map.scale(120,26,
relwidth=0.25,cex=0.6,
metric=TRUE,ratio=TRUE)
for (i in c(2:6,8:10)) {
map('worldHires',
region=countries[i],
fill=TRUE,add=TRUE,
col=colors[i])
}
Google maps are updated approximately every 9 months. These functions make if possible to capture a road map, satellite photo, or topographic map. Access to these maps requires the corresponding package.
You can install the package using the INSTALL button in the Packages in RStudio or copy and run the following code to download the package from the Internet. This copy only needs to be run once.
install.packages("RgoogleMaps")
This package is not part of the core of R and must be loaded into the working library once before using its functions. The code to do this is given below:
library(RgoogleMaps)
map1 <- GetMap(
"Bible House, Yangon, Myanmar",
destfile = "MyTile1.png",
size=c(640,640), zoom=18,
maptype = "roadmap")
This creates a graphic file (named MyTile1.png) which is stored in the project directory.
map
PlotOnStaticMap(map1)
map2 <- GetMap("Bible House, Yangon, Myanmar",
destfile = "MyTile2.png",size=c(640,640), zoom=18,
maptype = "satellite")
PlotOnStaticMap(map2)
GADM3 is a geographic information system (GIS) collection of the shapes of 4 levels of political boundaries for most countries of the world (namely country, province/state, district, townships). The databases are free for non-commercial uses and use longitude/latitude coordinate system. Each country collection is available in many versions so it is important to download the RDS version which was designed for use with R. The current version (2.8) was released in Nov 2015 and Version 3 is expected in Aug 2017. These maps are ideally suited for epidemic, political and economic research.
You can install the package using the INSTALL button in the Packages in RStudio or copy and run the following code to download the package from the Internet. This copy only needs to be run once.
install.packages("sp")
This package is not part of the core of R and must be loaded into the working library once before using its functions. The code to do this is given below:
library(sp)
gadm <- readRDS(
"datasets/MMR_adm2.rds")
colors = rainbow(100)
plot(gadm, col = colors, border = 'darkgrey')
gadm <- readRDS("datasets/MMR_adm1.rds")
colors = rep("#00FF0022",70)
colors[15] = "purple"
colors[gadm@data$NAME_1 ==
"Naypyitaw"] = "red"
plot(gadm, col = colors,
border = FALSE)
legend(99,19,c("Yangon","Naypyitaw"),fill=c("purple","red"))
The ADM file contains a database and by matching the ordering or by selecting a particular region it is possible to highlight selected regions of the country.
In this example, the various states and regions of Myanmar are colored according to the local populations.4
gadm <- readRDS(
"datasets/MMR_adm1.rds")
regions = c("Ayeyarwady","Bago",
"Chin", "Kachin", "Kayah",
"Kayin","Magway","Mandalay",
"Mon", "Naypyitaw","Rakhine",
"Sagaing", "Shan", "Tanintharyi",
"Yangon")
pop = c(6184829, 4867373,
478801, 1689441, 286627,
1574079, 3917055, 6165723,
2054393, 1160242, 3188807,
5325347, 5824432, 1408401,
1160242)
colors = rainbow(10)
plot(gadm, border = 'darkgrey',
col = colors[pop / 1000000],
main="Population by Region")
legend(101,20,
c("0.5M","1M","2M","4M","8M"),
fill = c(colors[1 + c(0.5,1,2,4,8)]))
Plotted graphs can be saved as graphic files that can be included in other documents and reports. The key is to activate a graphic output stream as shown in the following code block which re-plots the previous chart directly into a PNG file.
png("./myanmarpop.png",
width=480,height=800)
plot(gadm, border = 'darkgrey',
col = colors[pop / 1000000],
main="Population by Region")
legend(101,20,
c("0.5M","1M","2M","4M","8M"),
fill = c(colors[1 + c(0.5,1,2,4,8)]))
dev.off()
null device
1
The resulting graphic file can be loaded in the R notebook by referencing the external image.
Myanmar Population
The following example plots the location of Myanmar airports on a Google satellite image. The data was extracted from the master database of airport from Open Flight5
library(knitr)
airports = read.csv("datasets/myanmarairports.csv")
kable(airports)
name | call | lat | long |
---|---|---|---|
Kalay Airport | VYKL | 23.1888 | 94.0511 |
Mandalay International Airport | VYMD | 21.7022 | 95.9779 |
Myeik Airport | VYME | 12.4398 | 98.6215 |
Myitkyina Airport | VYMK | 25.3836 | 97.3519 |
Momeik Airport | VYMO | 23.0925 | 96.6453 |
Mong Hsat Airport | VYMS | 20.5168 | 99.2568 |
Nampong Air Base | VYNP | 25.3544 | 97.2952 |
Namsang Airport | VYNS | 20.8905 | 97.7359 |
Hpa-N Airport | VYPA | 16.8937 | 97.6746 |
Putao Airport | VYPT | 27.3299 | 97.4263 |
Pyay Airport | VYPY | 18.8245 | 95.2660 |
Shante Air Base | VYST | 20.9417 | 95.9145 |
Sittwe Airport | VYSW | 20.1327 | 92.8726 |
Thandwe Airport | VYTD | 18.4607 | 94.3001 |
Tachileik Airport | VYTL | 20.4838 | 99.9354 |
Taungoo Airport | VYTO | 19.0313 | 96.4012 |
Yangon International Airport | RGN | 16.9073 | 96.1332 |
These data can be plotted on maps generated within R as per the following code.
airports = read.csv("datasets/myanmarairports.csv")
map3 <- GetMap("Naypyitaw, Myanmar",
destfile = "MyTile2.png",size=c(640,640), zoom=5,
maptype = "satellite")
PlotOnStaticMap(map3,
airports$lat,airports$long,
col="yellow",pch=19)
```
The following code plots the same airport information on a silhouette of Myanmar.
gadm <- readRDS(
"datasets/MMR_adm0.rds")
plot(gadm, col = "#00FF0033",
border = 'darkgreen')
points(airports$long, airports$lat,
col="red",pch=19)
title("Commerical Airports of Myanmar")
While this technical note has only covered on the basics of mapping in R, there are many more advanced functions to allow the user to generate a wide assortment of maps and projections. The online help for each package can be found by double clicking on the package name in the Packages tab of RStudio. If you get stuck, you are welcome to contact me.
Enjoy!
RStudio Team (2015). RStudio: Integrated Development for R. RStudio, Inc., Boston, MA URL http://www.rstudio.com/products/RStudio/.↩
R Consortium (2017). R language downloaded from https://cran.r-project.org/mirrors.html↩
Global Administrative Areas (2015). GADM database of Global Administrative Areas, version 2.8. available online at http://www.gadm.org.↩
Wikipedia contributors (2017) Administrative divisions of Myanmar. in Wikipedia, The Free Encyclopedia. Available online↩
Open Flights (2012). Airport, airline and route data. Available online at https://openflights.org/data.html↩