Keep the visualization study, try to realize the following tasks:
- Plot one province or multiple provinces
- Add labels to capitals
- Plot the provincial population based on the latest census
- Plot the provincial population evoluation
- Realize the plots above using both
ggplot2
andggvis
Preparation
Load packages and modify local settings in case of Chinese characters not displaying.
Raw Data
Subset the polygons with AREA
larger than 0.005 and remove the N/A
.
|
|
Create a data.frame
called cnmapdf
which contains id
, prov_en
and prov_cn
and key map plotting info:
|
|
You can also use dplyr
to realize the last step, but it is much slower.
|
|
Coordinates of Province Capitals
Set the capital coordinates of each provinces.
|
|
Plot Map for One Province
Let’s try to plot Shanghai first.
|
|
Plot Map for Several Provinces
To plot a group of provinces, say Jiangsu, Zhejiang and Shanghai, the Yangtze River Delta.
|
|
Why do we declare aes()
inside geom_polygon()
instead of ggplot()
? Run the code below and try to move it to the aesthetic mapping in ggplot()
and you will find the answer.
It’s apparently a bit complicated using annotate()
to add labels to provinces. We can do that this way:
|
|
How can we do that via ggvis
?
In the current version (0.4.2) there is no function like ggplot2::ggtitle()
to directly add a title to a ggvis
package. So we can define one ourselves.
|
|
Then apply add_title()
to our plot
|
|
Combining Population Data
Add Population Data
Macau is not specified in the raw data, therefore we only plot the other 33 provinces / municipalities / autonomous regions.
Population data is scrapped from Wikipedia based on Census. The population Hong Kong and Taiwan are independently collected, so I pick the data in the most adjacent years.
You could download the .xlsx
raw data DemoChinaRaw.xlsx and the .csv
cleaned data DemoChina.csv.
Need to pay attention to the changes of provinces:
- Chongqing was a city of Sichuan Province and became a municipality in 1997.
- Hainan was a part of Guangdong Province and became an independent province in 1988. This is not reflected in the result of Census in 1990).
- From 1949 to now, Tianjin is a municipality but once was a city of Hebei Province between 1958 and 1967.
|
|
Plot the Population per Province by ggplot
Combine the population from Census in 2010 with map data:
|
|
To make the heat map by another color scale:
|
|
Plot the Population per Province by ggvis
Let’s try ggvis
:
|
|
Plot the Population per Year
To check the population evolution per each census by ggplot2
.
|
|
Based on the census in 1982 and 1990, the population in Sichuan Province is even higher than in the following years. Should be aware that before the census in 2000, Chongqing is a city of Sichuan. That is how the illusion comes.
It is a shame that ggvis
cannot do faceting yet.