Lecture 9: Plots with Maps

HKU STAT3622 Data Visualization by Dr. Aijun Zhang
2 November 2017


What's covered in this lecture?

  • Display Maps in R
  • Show Data on Maps
  • Examples with HK geographical data

Map Visualization

  • Geographical/spatial data visualization
  • Spatial data structure
  • Static and interactive maps
  • R packages: maps, sp, ggplot2, plotly, leaflet …

World Map in R

library(maps)
map('world', fill=TRUE, border='darkgray',  col='gray',   bg='white')

plot of chunk unnamed-chunk-1

World Map by ggplot2

library(ggplot2)
thismap = map_data('world')
ggplot(thismap, aes(long, lat, group=group)) +
  geom_polygon(fill='yellow', colour="green") 

plot of chunk unnamed-chunk-2

Country Maps

library(mapdata)
thismap = map("china", plot=F)
ggplot(thismap, aes(long, lat, group=group)) +
  geom_path() 

plot of chunk unnamed-chunk-3

thismap = map_data("state")
ggplot(thismap, aes(long, lat, group=group, fill=region)) +
  geom_polygon(show.legend = F) 

plot of chunk unnamed-chunk-4

Spatial data structure

library(sp)
hkmap = readRDS("HKG_adm1.rds")  
class(hkmap)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"

plot of chunk unnamed-chunk-6

Hong Kong SAR Map

ggplot(hkmap, aes(long, lat, group=group, fill=id)) +
  geom_polygon(show.legend = F)

plot of chunk unnamed-chunk-7

Hong Kong Regions and Districts

wikidata = read.csv("HK18Districts.csv") 
wikidata 
              District Population   Area  Density Region Code
1  Central and Western     244600  12.44 19983.92     HK   CW
2              Eastern     574500  18.56 31217.67     HK   EA
3             Southern     269200  38.85  6962.68     HK   SO
4             Wan Chai     150900   9.83 15300.10     HK   WC
5         Sham Shui Po     390600   9.35 41529.41     KL   SS
6         Kowloon City     405400  10.02 40194.70     KL   KC
7            Kwun Tong     641100  11.27 56779.05     KL   KU
8         Wong Tai Sin     426200   9.30 45645.16     KL   WT
9        Yau Tsim Mong     318100   6.99 44864.09     KL   YT
10             Islands     146900 175.12   825.14     NT   IS
11          Kwai Tsing     507100  23.34 21503.86     NT   KI
12               North     310800 136.61  2220.19     NT   NO
13            Sai Kung     448600 129.65  3460.08     NT   SK
14             Sha Tin     648200  68.71  9433.85     NT   ST
15              Tai Po     307100 136.15  2220.35     NT   TP
16           Tsuen Wan     303600  61.71  4887.38     NT   TW
17            Tuen Mun     495900  82.89  5889.38     NT   TM
18           Yuen Long     607200 138.46  4297.99     NT   YL

Hong Kong Regions and Districts

hkmapmeta = data.frame(mapid=hkmap$ID_1, Code=hkmap$HASC_1)
hkmapmeta$Code = gsub('HK.', '', as.character(hkmapmeta$Code))
hkmapmeta = merge(wikidata, hkmapmeta, by="Code", sort=FALSE)
hkmapmeta 
   Code            District Population   Area  Density Region mapid
1    CW Central and Western     244600  12.44 19983.92     HK     1
2    EA             Eastern     574500  18.56 31217.67     HK     2
3    SO            Southern     269200  38.85  6962.68     HK    11
4    WC            Wan Chai     150900   9.83 15300.10     HK    15
5    SS        Sham Shui Po     390600   9.35 41529.41     KL    10
6    KC        Kowloon City     405400  10.02 40194.70     KL     4
7    KU           Kwun Tong     641100  11.27 56779.05     KL     6
8    WT        Wong Tai Sin     426200   9.30 45645.16     KL    16
9    YT       Yau Tsim Mong     318100   6.99 44864.09     KL    17
10   IS             Islands     146900 175.12   825.14     NT     3
11   KI          Kwai Tsing     507100  23.34 21503.86     NT     5
12   NO               North     310800 136.61  2220.19     NT     7
13   SK            Sai Kung     448600 129.65  3460.08     NT     8
14   ST             Sha Tin     648200  68.71  9433.85     NT     9
15   TP              Tai Po     307100 136.15  2220.35     NT    12
16   TW           Tsuen Wan     303600  61.71  4887.38     NT    13
17   TM            Tuen Mun     495900  82.89  5889.38     NT    14
18   YL           Yuen Long     607200 138.46  4297.99     NT    18

Hong Kong Regions and Districts

hkmapdf = fortify(hkmap) 
hkmapdf = merge(hkmapdf,hkmapmeta, by.x="id", by.y="mapid")
kable(hkmapdf[1:5,])
id long lat order hole piece group Code District Population Area Density Region
1 114.1670 22.28153 1 FALSE 1 1.1 CW Central and Western 244600 12.44 19983.92 HK
1 114.1671 22.28086 2 FALSE 1 1.1 CW Central and Western 244600 12.44 19983.92 HK
1 114.1672 22.28038 3 FALSE 1 1.1 CW Central and Western 244600 12.44 19983.92 HK
1 114.1682 22.27885 4 FALSE 1 1.1 CW Central and Western 244600 12.44 19983.92 HK
1 114.1683 22.27865 5 FALSE 1 1.1 CW Central and Western 244600 12.44 19983.92 HK
  • ggplot2::fortify converts “sp” object to data.frame

Hong Kong Regions and Districts: 3 regions

ggplot(hkmapdf, aes(long, lat, group=group, fill=Region)) +
  geom_polygon()

plot of chunk unnamed-chunk-11

Hong Kong Regions and Districts

ggplot(hkmapdf, aes(long, lat, group=group, fill=Code)) +
  geom_polygon() +
  guides(fill=guide_legend(title='18 Districts', ncol = 2)) 

plot of chunk unnamed-chunk-12

Showing Data on Map: Population by Districts

ggplot(hkmapdf, aes(long, lat, group=group, fill=Population)) +
  geom_polygon() + 
  ggtitle("Population by Districts") 

plot of chunk unnamed-chunk-13

Showing Data on Map: Population Density by Districts

ggplot(hkmapdf, aes(long, lat, group=group, fill=Density)) +
  geom_polygon() +
  ggtitle("Population Density by Districts")