class: center, middle, inverse, title-slide # STAT3622 Data Visualization (Lecture 8) ## Geographical Data Visualization ###
Dr. Aijun Zhang
The University of Hong Kong ### 31 March 2020 --- # What's covered in this lecture? <img style="float: right; width: 400px; padding:0 50px 0 0;" src="HKMap.png"> - Display Maps in R - World/Countries - Hong Kong SAR - R:leaflet Package - Data Visualization on Maps - Case Study: HK Home Prices --- class: center, middle # 1. Display Maps in R --- # Map Visualization - Geographical/spatial data visualization - Spatial data structure - Static and interactive maps - R packages: maps, sp, ggplot2, plotly, leaflet … --- # World Map in R ```r library(maps) map('world', fill=TRUE, border='black', col='gray', bg='white') ``` <img src="index_files/figure-html/unnamed-chunk-1-1.png" style="display: block; margin: auto;" /> --- # World Map by R:ggplot2 ```r library(ggplot2) worldmap = map_data('world') ggplot(worldmap, aes(long, lat, group=group)) + geom_polygon(fill='yellow', colour="green") + theme_bw() ``` <img src="index_files/figure-html/unnamed-chunk-2-1.png" style="display: block; margin: auto;" /> --- # Map Data Structure ```r str(worldmap) ``` ``` ## 'data.frame': 99338 obs. of 6 variables: ## $ long : num -69.9 -69.9 -69.9 -70 -70.1 ... ## $ lat : num 12.5 12.4 12.4 12.5 12.5 ... ## $ group : num 1 1 1 1 1 1 1 1 1 1 ... ## $ order : int 1 2 3 4 5 6 7 8 9 10 ... ## $ region : chr "Aruba" "Aruba" "Aruba" "Aruba" ... ## $ subregion: chr NA NA NA NA ... ``` ```r head(worldmap) ``` ``` ## long lat group order region subregion ## 1 -69.89912 12.45200 1 1 Aruba <NA> ## 2 -69.89571 12.42300 1 2 Aruba <NA> ## 3 -69.94219 12.43853 1 3 Aruba <NA> ## 4 -70.00415 12.50049 1 4 Aruba <NA> ## 5 -70.06612 12.54697 1 5 Aruba <NA> ## 6 -70.05088 12.59707 1 6 Aruba <NA> ``` --- # Country Maps: US ```r thismap = map_data("state") ggplot(thismap, aes(long, lat, group=group, fill=region)) + geom_polygon(show.legend = F) ``` <img src="index_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> --- # Country Maps: China ```r library(mapdata) # map data for more countries thismap = map("china", plot=F) ggplot(thismap, aes(long, lat, group=group)) + geom_path() + ylim(15,55) ``` <img src="index_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> - A tutorial in Chinese: https://cosx.org/2009/07/drawing-china-map-using-r/ --- # Hong Kong Map Data - Download map database (.rds) source: [http://gadm.org/country](http://gadm.org/country) ```r library(sp) hkmap = readRDS("HKG_adm1.rds") class(hkmap) ``` ``` ## [1] "SpatialPolygonsDataFrame" ## attr(,"package") ## [1] "sp" ``` - ggplot2::fortify converts "sp" object to data.frame ```r library(ggplot2) hkmapdf = fortify(hkmap) head(hkmapdf,3) ``` ``` ## long lat order hole piece id group ## 1 114.1670 22.28153 1 FALSE 1 1 1.1 ## 2 114.1671 22.28086 2 FALSE 1 1 1.1 ## 3 114.1672 22.28038 3 FALSE 1 1 1.1 ``` --- # Hong Kong SAR Map ```r ggplot(hkmapdf, aes(long, lat, group=group, fill=id)) + geom_polygon(show.legend = F) ``` <img src="index_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- # Extra data from Wikipedia - HK population by districts: https://en.wikipedia.org/wiki/Districts_of_Hong_Kong - Copy and paste datatable to Excel plus some manual editing ```r wikidata = read.csv("HK18Districts.csv"); library(knitr); kable(wikidata, format="html") ``` <table> <thead> <tr> <th style="text-align:left;"> District </th> <th style="text-align:right;"> Population </th> <th style="text-align:right;"> Area </th> <th style="text-align:right;"> Density </th> <th style="text-align:left;"> Region </th> <th style="text-align:left;"> Code </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Central and Western </td> <td style="text-align:right;"> 244600 </td> <td style="text-align:right;"> 12.44 </td> <td style="text-align:right;"> 19983.92 </td> <td style="text-align:left;"> HK </td> <td style="text-align:left;"> CW </td> </tr> <tr> <td style="text-align:left;"> Eastern </td> <td style="text-align:right;"> 574500 </td> <td style="text-align:right;"> 18.56 </td> <td style="text-align:right;"> 31217.67 </td> <td style="text-align:left;"> HK </td> <td style="text-align:left;"> EA </td> </tr> <tr> <td style="text-align:left;"> Southern </td> <td style="text-align:right;"> 269200 </td> <td style="text-align:right;"> 38.85 </td> <td style="text-align:right;"> 6962.68 </td> <td style="text-align:left;"> HK </td> <td style="text-align:left;"> SO </td> </tr> <tr> <td style="text-align:left;"> Wan Chai </td> <td style="text-align:right;"> 150900 </td> <td style="text-align:right;"> 9.83 </td> <td style="text-align:right;"> 15300.10 </td> <td style="text-align:left;"> HK </td> <td style="text-align:left;"> WC </td> </tr> <tr> <td style="text-align:left;"> Sham Shui Po </td> <td style="text-align:right;"> 390600 </td> <td style="text-align:right;"> 9.35 </td> <td style="text-align:right;"> 41529.41 </td> <td style="text-align:left;"> KL </td> <td style="text-align:left;"> SS </td> </tr> <tr> <td style="text-align:left;"> Kowloon City </td> <td style="text-align:right;"> 405400 </td> <td style="text-align:right;"> 10.02 </td> <td style="text-align:right;"> 40194.70 </td> <td style="text-align:left;"> KL </td> <td style="text-align:left;"> KC </td> </tr> <tr> <td style="text-align:left;"> Kwun Tong </td> <td style="text-align:right;"> 641100 </td> <td style="text-align:right;"> 11.27 </td> <td style="text-align:right;"> 56779.05 </td> <td style="text-align:left;"> KL </td> <td style="text-align:left;"> KU </td> </tr> <tr> <td style="text-align:left;"> Wong Tai Sin </td> <td style="text-align:right;"> 426200 </td> <td style="text-align:right;"> 9.30 </td> <td style="text-align:right;"> 45645.16 </td> <td style="text-align:left;"> KL </td> <td style="text-align:left;"> WT </td> </tr> <tr> <td style="text-align:left;"> Yau Tsim Mong </td> <td style="text-align:right;"> 318100 </td> <td style="text-align:right;"> 6.99 </td> <td style="text-align:right;"> 44864.09 </td> <td style="text-align:left;"> KL </td> <td style="text-align:left;"> YT </td> </tr> <tr> <td style="text-align:left;"> Islands </td> <td style="text-align:right;"> 146900 </td> <td style="text-align:right;"> 175.12 </td> <td style="text-align:right;"> 825.14 </td> <td style="text-align:left;"> NT </td> <td style="text-align:left;"> IS </td> </tr> <tr> <td style="text-align:left;"> Kwai Tsing </td> <td style="text-align:right;"> 507100 </td> <td style="text-align:right;"> 23.34 </td> <td style="text-align:right;"> 21503.86 </td> <td style="text-align:left;"> NT </td> <td style="text-align:left;"> KI </td> </tr> <tr> <td style="text-align:left;"> North </td> <td style="text-align:right;"> 310800 </td> <td style="text-align:right;"> 136.61 </td> <td style="text-align:right;"> 2220.19 </td> <td style="text-align:left;"> NT </td> <td style="text-align:left;"> NO </td> </tr> <tr> <td style="text-align:left;"> Sai Kung </td> <td style="text-align:right;"> 448600 </td> <td style="text-align:right;"> 129.65 </td> <td style="text-align:right;"> 3460.08 </td> <td style="text-align:left;"> NT </td> <td style="text-align:left;"> SK </td> </tr> <tr> <td style="text-align:left;"> Sha Tin </td> <td style="text-align:right;"> 648200 </td> <td style="text-align:right;"> 68.71 </td> <td style="text-align:right;"> 9433.85 </td> <td style="text-align:left;"> NT </td> <td style="text-align:left;"> ST </td> </tr> <tr> <td style="text-align:left;"> Tai Po </td> <td style="text-align:right;"> 307100 </td> <td style="text-align:right;"> 136.15 </td> <td style="text-align:right;"> 2220.35 </td> <td style="text-align:left;"> NT </td> <td style="text-align:left;"> TP </td> </tr> <tr> <td style="text-align:left;"> Tsuen Wan </td> <td style="text-align:right;"> 303600 </td> <td style="text-align:right;"> 61.71 </td> <td style="text-align:right;"> 4887.38 </td> <td style="text-align:left;"> NT </td> <td style="text-align:left;"> TW </td> </tr> <tr> <td style="text-align:left;"> Tuen Mun </td> <td style="text-align:right;"> 495900 </td> <td style="text-align:right;"> 82.89 </td> <td style="text-align:right;"> 5889.38 </td> <td style="text-align:left;"> NT </td> <td style="text-align:left;"> TM </td> </tr> <tr> <td style="text-align:left;"> Yuen Long </td> <td style="text-align:right;"> 607200 </td> <td style="text-align:right;"> 138.46 </td> <td style="text-align:right;"> 4297.99 </td> <td style="text-align:left;"> NT </td> <td style="text-align:left;"> YL </td> </tr> </tbody> </table> --- # Map Wikidata with HKmap$HASC_1 ```r 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) kable(hkmapmeta, format="html") ``` <table> <thead> <tr> <th style="text-align:left;"> Code </th> <th style="text-align:left;"> District </th> <th style="text-align:right;"> Population </th> <th style="text-align:right;"> Area </th> <th style="text-align:right;"> Density </th> <th style="text-align:left;"> Region </th> <th style="text-align:right;"> mapid </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> CW </td> <td style="text-align:left;"> Central and Western </td> <td style="text-align:right;"> 244600 </td> <td style="text-align:right;"> 12.44 </td> <td style="text-align:right;"> 19983.92 </td> <td style="text-align:left;"> HK </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> EA </td> <td style="text-align:left;"> Eastern </td> <td style="text-align:right;"> 574500 </td> <td style="text-align:right;"> 18.56 </td> <td style="text-align:right;"> 31217.67 </td> <td style="text-align:left;"> HK </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> SO </td> <td style="text-align:left;"> Southern </td> <td style="text-align:right;"> 269200 </td> <td style="text-align:right;"> 38.85 </td> <td style="text-align:right;"> 6962.68 </td> <td style="text-align:left;"> HK </td> <td style="text-align:right;"> 11 </td> </tr> <tr> <td style="text-align:left;"> WC </td> <td style="text-align:left;"> Wan Chai </td> <td style="text-align:right;"> 150900 </td> <td style="text-align:right;"> 9.83 </td> <td style="text-align:right;"> 15300.10 </td> <td style="text-align:left;"> HK </td> <td style="text-align:right;"> 15 </td> </tr> <tr> <td style="text-align:left;"> SS </td> <td style="text-align:left;"> Sham Shui Po </td> <td style="text-align:right;"> 390600 </td> <td style="text-align:right;"> 9.35 </td> <td style="text-align:right;"> 41529.41 </td> <td style="text-align:left;"> KL </td> <td style="text-align:right;"> 10 </td> </tr> <tr> <td style="text-align:left;"> KC </td> <td style="text-align:left;"> Kowloon City </td> <td style="text-align:right;"> 405400 </td> <td style="text-align:right;"> 10.02 </td> <td style="text-align:right;"> 40194.70 </td> <td style="text-align:left;"> KL </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> KU </td> <td style="text-align:left;"> Kwun Tong </td> <td style="text-align:right;"> 641100 </td> <td style="text-align:right;"> 11.27 </td> <td style="text-align:right;"> 56779.05 </td> <td style="text-align:left;"> KL </td> <td style="text-align:right;"> 6 </td> </tr> <tr> <td style="text-align:left;"> WT </td> <td style="text-align:left;"> Wong Tai Sin </td> <td style="text-align:right;"> 426200 </td> <td style="text-align:right;"> 9.30 </td> <td style="text-align:right;"> 45645.16 </td> <td style="text-align:left;"> KL </td> <td style="text-align:right;"> 16 </td> </tr> <tr> <td style="text-align:left;"> YT </td> <td style="text-align:left;"> Yau Tsim Mong </td> <td style="text-align:right;"> 318100 </td> <td style="text-align:right;"> 6.99 </td> <td style="text-align:right;"> 44864.09 </td> <td style="text-align:left;"> KL </td> <td style="text-align:right;"> 17 </td> </tr> <tr> <td style="text-align:left;"> IS </td> <td style="text-align:left;"> Islands </td> <td style="text-align:right;"> 146900 </td> <td style="text-align:right;"> 175.12 </td> <td style="text-align:right;"> 825.14 </td> <td style="text-align:left;"> NT </td> <td style="text-align:right;"> 3 </td> </tr> <tr> <td style="text-align:left;"> KI </td> <td style="text-align:left;"> Kwai Tsing </td> <td style="text-align:right;"> 507100 </td> <td style="text-align:right;"> 23.34 </td> <td style="text-align:right;"> 21503.86 </td> <td style="text-align:left;"> NT </td> <td style="text-align:right;"> 5 </td> </tr> <tr> <td style="text-align:left;"> NO </td> <td style="text-align:left;"> North </td> <td style="text-align:right;"> 310800 </td> <td style="text-align:right;"> 136.61 </td> <td style="text-align:right;"> 2220.19 </td> <td style="text-align:left;"> NT </td> <td style="text-align:right;"> 7 </td> </tr> <tr> <td style="text-align:left;"> SK </td> <td style="text-align:left;"> Sai Kung </td> <td style="text-align:right;"> 448600 </td> <td style="text-align:right;"> 129.65 </td> <td style="text-align:right;"> 3460.08 </td> <td style="text-align:left;"> NT </td> <td style="text-align:right;"> 8 </td> </tr> <tr> <td style="text-align:left;"> ST </td> <td style="text-align:left;"> Sha Tin </td> <td style="text-align:right;"> 648200 </td> <td style="text-align:right;"> 68.71 </td> <td style="text-align:right;"> 9433.85 </td> <td style="text-align:left;"> NT </td> <td style="text-align:right;"> 9 </td> </tr> <tr> <td style="text-align:left;"> TP </td> <td style="text-align:left;"> Tai Po </td> <td style="text-align:right;"> 307100 </td> <td style="text-align:right;"> 136.15 </td> <td style="text-align:right;"> 2220.35 </td> <td style="text-align:left;"> NT </td> <td style="text-align:right;"> 12 </td> </tr> <tr> <td style="text-align:left;"> TW </td> <td style="text-align:left;"> Tsuen Wan </td> <td style="text-align:right;"> 303600 </td> <td style="text-align:right;"> 61.71 </td> <td style="text-align:right;"> 4887.38 </td> <td style="text-align:left;"> NT </td> <td style="text-align:right;"> 13 </td> </tr> <tr> <td style="text-align:left;"> TM </td> <td style="text-align:left;"> Tuen Mun </td> <td style="text-align:right;"> 495900 </td> <td style="text-align:right;"> 82.89 </td> <td style="text-align:right;"> 5889.38 </td> <td style="text-align:left;"> NT </td> <td style="text-align:right;"> 14 </td> </tr> <tr> <td style="text-align:left;"> YL </td> <td style="text-align:left;"> Yuen Long </td> <td style="text-align:right;"> 607200 </td> <td style="text-align:right;"> 138.46 </td> <td style="text-align:right;"> 4297.99 </td> <td style="text-align:left;"> NT </td> <td style="text-align:right;"> 18 </td> </tr> </tbody> </table> --- # Merge HKMap DataFrame with Wikidata ```r hkmapdf = merge(hkmapdf,hkmapmeta, by.x="id", by.y="mapid") head(hkmapdf) ``` ``` ## id long lat order hole piece group Code District Population Area ## 1 1 114.1670 22.28153 1 FALSE 1 1.1 CW Central and Western 244600 12.44 ## 2 1 114.1671 22.28086 2 FALSE 1 1.1 CW Central and Western 244600 12.44 ## 3 1 114.1672 22.28038 3 FALSE 1 1.1 CW Central and Western 244600 12.44 ## 4 1 114.1682 22.27885 4 FALSE 1 1.1 CW Central and Western 244600 12.44 ## 5 1 114.1683 22.27865 5 FALSE 1 1.1 CW Central and Western 244600 12.44 ## 6 1 114.1683 22.27623 6 FALSE 1 1.1 CW Central and Western 244600 12.44 ## Density Region ## 1 19983.92 HK ## 2 19983.92 HK ## 3 19983.92 HK ## 4 19983.92 HK ## 5 19983.92 HK ## 6 19983.92 HK ``` --- # Hong Kong Map: 3 Regions ```r ggplot(hkmapdf, aes(long, lat, group=group, fill=Region)) + geom_polygon() ``` <img src="index_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> --- # Hong Kong Map: 18 Districts ```r ggplot(hkmapdf, aes(long, lat, group=group, fill=Code)) + geom_polygon() + guides(fill=guide_legend(title='18 Districts', ncol = 2)) ``` <img src="index_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> <!-- --- --> <!-- # Interactive Maps by R:leaflet --> <!-- ```{r fig.align="center", fig.asp=0.5, fig.width=10} --> <!-- library(leaflet) --> <!-- leaflet() %>% addTiles() %>% # Add map tiles (Default: OpenStreetMap) --> <!-- addMarkers(lng=114.137426, lat=22.282793, popup="The University of Hong Kong") --> <!-- ``` --> <!-- --- --> <!-- # Adding Map Markers: EV Charging Stations --> <!-- ```{r fig.align="center", fig.width=10, fig.asp=0.8, echo = T} --> <!-- mapdata = read.csv("hkecharge.csv") --> <!-- kable(t(mapdata[1:3,]), format="html") # the first record --> <!-- ``` --> <!-- - Download the data from [https://DATA.GOV.HK/](https://data.gov.hk/en-data/dataset/hkelectric-tnd_cs_ci-hkelectric-ev-location) --> <!-- --- --> <!-- # Adding Map Markers: EV Charging Stations --> <!-- ```{r fig.asp=0.5, fig.width=10} --> <!-- leaflet(data = mapdata) %>% addTiles() %>% --> <!-- addMarkers(~Longitude, ~Latitude, popup = ~as.character(Address)) %>% --> <!-- setView(lng = 114.183413 , lat = 22.248648, zoom=12) --> <!-- ``` --> --- class: center, middle # 2. Data Visualization on Maps --- # HK Population by Districts ```r ggplot(hkmapdf, aes(long, lat, group=group, fill=Population)) + geom_polygon() + ggtitle("Population by Districts") ``` <img src="index_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> --- # HK Population Density by Districts ```r ggplot(hkmapdf, aes(long, lat, group=group, fill=Density)) + geom_polygon() + ggtitle("Population Density by Districts") ``` <img src="index_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" /> --- # HK Population Density by Districts - Customized color bar by manually setting `scale_fill_gradient` ```r ggplot(hkmapdf, aes(long, lat, group=group, fill=Density)) + geom_polygon() + scale_fill_gradient(limits=range(hkmapdf$Density), low="white", high="red") + ggtitle("Population Density by Districts") ``` <img src="index_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto;" /> --- class: center, middle # 3. Case Study: HK Home Prices <img src="HKHousing.jpg" width="500px" style="display: block; margin: auto;" /> <br /> Purpose of study: to demonstrate geographical/spatiotemporal data visualization. Click the [HTML](Lecture8_HomePrice.html) file generated by R markdown. --- class: center, middle # Thank you! Q&A or Email ajzhang@umich.edu。