Require packages and summary population data

Taiwan population data source is from the following website: https://zh.wikipedia.org/wiki/臺灣行政區人口列表

require("dplyr")
require("stringr")
require("data.table")
require("ggplot2")
require("maptools")
require("knitr")
require("kableExtra")
df <- fread("D:\\tw_population.csv", h = T)
df <- data.frame(df)
kable(df)
縣市 類別 人口
新北市 直轄市 3985698
臺中市 直轄市 2783298
高雄市 直轄市 2776791
臺北市 直轄市 2683202
桃園市 直轄市 2181470
臺南市 直轄市 1886387
彰化縣 1282934
屏東縣 830697
雲林縣 691021
苗栗縣 554652
新竹縣 551447
嘉義縣 511797
南投縣 501757
宜蘭縣 456756
新竹市 440409
基隆市 371669
花蓮縣 329462
嘉義市 269364
臺東縣 219686
金門縣 137042
澎湖縣 103956
連江縣 12823
summary(df)
##      縣市               類別                人口        
##  Length:22          Length:22          Min.   :  12823  
##  Class :character   Class :character   1st Qu.: 340014  
##  Mode  :character   Mode  :character   Median : 531622  
##                                        Mean   :1071014  
##                                        3rd Qu.:1735524  
##                                        Max.   :3985698

Popution visualization using a bar plot

The following steps aims to make a better plot. 1. transform population unit to 10,000 units 2. flip the coordinate 3. sort the cities by its population

colnames(df) <- c("city","category","population")
ggplot(df, aes(x = reorder(city, population), y = population/10000, fill = category)) + 
  geom_bar(stat="identity") +
  coord_flip() + 
  labs(title = "台灣縣市人口分布圖", x = "縣市", y = "人口數(萬)") 

Read and plot Taiwan map data

Taiwan population data source is from the following website: http://www.gadm.org/country (shapefile form)

  1. Read shape file
  2. Tranform shp to dataframe
  3. Plot Taiwan Map by ggplot2
taiwan_shp <- readShapeSpatial("D:\\TWN_adm_shp\\TWN_adm2.shp")
## Warning: use rgdal::readOGR or sf::st_read

## Warning: use rgdal::readOGR or sf::st_read
taiwan_map <- fortify(taiwan_shp)
## Regions defined for each Polygons
ggplot(taiwan_map, aes(x = long, y = lat, group=group)) +
  geom_path() + 
  coord_map()

Translate City Names from English to Chinese

#Sys.setlocale('LC_ALL','C')
print(as.character(taiwan_shp$NAME_2))
##  [1] "Kinmen"                    "Lienkiang (Matsu Islands)"
##  [3] "Hsinchu City"              "Kaohsiung"                
##  [5] "New Taipei City"           "Taichung"                 
##  [7] "Tainan"                    "Taipei"                   
##  [9] "Taoyuan"                   "Changhua"                 
## [11] "Chiayi City"               "Chiayi County"            
## [13] "Hsinchu County"            "Hualien"                  
## [15] "Keelung"                   "Miaoli"                   
## [17] "Nantou"                    "Penghu"                   
## [19] "Pingtung"                  "Taitung"                  
## [21] "Yilan"                     "Yulin"
chinese_name <- c("金門縣", "連江縣", "新竹市", "高雄市",
                  "新北市", "臺中市", "臺南市", "臺北市",
                  "桃園市", "彰化縣", "嘉義市", "嘉義縣",
                  "新竹縣", "花蓮縣", "基隆市", "苗栗縣",
                  "南投縣", "澎湖縣", "屏東縣", "臺東縣",
                  "宜蘭縣", "雲林縣")
print(chinese_name)
##  [1] "金門縣" "連江縣" "新竹市" "高雄市" "新北市" "臺中市" "臺南市"
##  [8] "臺北市" "桃園市" "彰化縣" "嘉義市" "嘉義縣" "新竹縣" "花蓮縣"
## [15] "基隆市" "苗栗縣" "南投縣" "澎湖縣" "屏東縣" "臺東縣" "宜蘭縣"
## [22] "雲林縣"

Combine these two Dataset for plotting

  1. create a new data of cities
  2. map population from “df” to “taiwan_shp”
  3. merge two these two datasets, “mydata” and “taiwan_shp”
mydata <- data.frame(NAME_1=taiwan_shp$NAME_2,
                     NAME_2=chinese_name,
                     id=taiwan_shp$ID_2)
mydata$population <- 0
for(i in 1:nrow(mydata)){
  mydata$population[i] <- df$population[which(df$city == mydata$NAME_2[i])]
}
taiwan_map$id <- as.character(as.integer(taiwan_map$id)+1)
final.plot<-merge(taiwan_map,mydata,by="id",all.x=T)
head(final.plot)
##   id     long      lat order  hole piece group NAME_1 NAME_2 population
## 1  1 118.4654 24.44180     1 FALSE     1   0.1 Kinmen 金門縣     137042
## 2  1 118.4647 24.44181     2 FALSE     1   0.1 Kinmen 金門縣     137042
## 3  1 118.4646 24.44181     3 FALSE     1   0.1 Kinmen 金門縣     137042
## 4  1 118.4646 24.44153     4 FALSE     1   0.1 Kinmen 金門縣     137042
## 5  1 118.4640 24.44153     5 FALSE     1   0.1 Kinmen 金門縣     137042
## 6  1 118.4640 24.44125     6 FALSE     1   0.1 Kinmen 金門縣     137042

Plotting Taiwan Population Distribution Map

library(RColorBrewer) #配色用brewer.pal( 9 , "Reds" )
library(mapproj)
twcmap <- ggplot() +
  geom_polygon(data = final.plot, 
               aes(x = long, y = lat, group = group, 
                   fill = population/10000), 
               color = "black", size = 0.25) + 
  
  coord_map()+#維持地圖比例
  scale_fill_gradientn(colours = brewer.pal(9,"Reds"), name = "人口(萬)")+
  #theme_void()+
  labs(title="台灣縣市人口分佈圖", x ="經度", y = "緯度")

twcmap

Add City Locations Points with their names

  1. search locations by google
  2. add points and labels to the plotting
  3. Another way to label
require(ggmap)
require(proto)
city <- geocode(str_c(as.character(mydata$NAME_2), ", 台灣"),
                source = "google")
city$name <- as.character(mydata$NAME_2)

city <- filter(city, lon !="")
twcmap + geom_point(aes(x=lon,y=lat),
                    color="forestgreen", size = 1.5, data=city) +
  geom_text(aes(x=lon,y=lat,label=name), data=city)

require(ggrepel)
twcmap + geom_label_repel(aes(x=lon,y=lat, label = name, alpha = 1),
                          data = city)