an example of using static map in R.

Frequently we demonstrate data based on a location. In this case, using a map as background would be helpful. In this example, national parks in British Columbia of Canada will be shown on a static BC map.

Step 1: install required package for obtaining google map

> install.packages("ggmap")

Step 2: load package “RgoogleMaps”

> library("ggmap", lib.loc="~/R/win-library/3.4")

Step 3: get BC map from Google

> bc.map <- get_map("BC, Canada", zoom = 5)

Step 4: plot map on the screen

> ggmap(bc.map)

Step 5: show the data file of BC national parks

> bc.park <- read.csv("BC_national_parks.csv", header = T)
> bc.park
##                                 Name Established.Year Area.km2 Latitude
## 1              Glacier National Park             1886     1349 51.30000
## 2 Gulf Islands National Park Reserve             2003       36 48.85056
## 3 Gwaii Haanas National Park Reserve             1988     1495 52.38917
## 4             Kootenay National Park             1920     1406 50.88306
## 5     Mount Revelstoke National Park             1914      260 51.08583
## 6  Pacific Rim National Park Reserve             1970      511 48.63611
## 7                 Yoho National Park             1886     1313 51.39528
##   Longitude
## 1 -117.5186
## 2 -123.4478
## 3 -131.4711
## 4 -116.0492
## 5 -118.0656
## 6 -124.7692
## 7 -116.4867

Step 6: load package “ggplot2”

> library("ggplot2", lib.loc="~/R/win-library/3.4")

Step 7: add points of national park on the map

> map1 <- ggmap(bc.map)
> map1 + geom_point(data=bc.park, aes(Longitude, Latitude), size=2, colour= "red")

Note: this is only a rough example of using map and loacation in R. Actually it leaves much to improve.