ggplot can draw the map in a more decent way. But the data ggplot deal with will not be our original data. Instead, it will first transform the data using the function
Now we use ggplot to draw the map:
Let me explain these commands one by one.
The first line,
ggplot() function’s arguments, includes the data and the aesthetic options. Here we use the original dataset china_map1, but
ggplot() will transform it to
fortify(china_map1) automatically, with the message Regions “defined for each Polygons”. That is why we cannot see long and lat variables in
china_map2 = china_map1@data, they are in the dataset
china_map3 = fortify(china_map1).
The second line,
geom_path() is used instead of
geom_point(), since we need to connect the points by the order in the data frame, not their x-lab position in the diagram. The color option set the color of the border.
The third line,
geom_polygon just set the filled in color for each polygon.
But the map is a little wired: It does not look like the map we see everyday. This can be achieved by adding this command
Province Map in different colors
First, I create a big dataset, which includes all the information in china_map2 and china_map3.
Now we can have a preview of china_map4:
Now we paint different colors to different provinces:
Province Map in different colors with population
The previous diagram is filled with names, 33 distinct colors. Now I am going to fill the regions with population. The data of China’s population comes from the 2010 national six census.
After the data processing, we can draw now:
Map for a particular province from country level
Here we use the province of Zhejiang for test.
China’s Provice Map
It is time to go deeper. In this part, I will deal with map data in the level of province.
One place, one polygon
First we will see the simple case: the place only has one polygon attached. The place we choose will be Beijing.
Here we need to know some knowledge about ADCODE99. The first two digits, 11, is referred to province, here is Beijing. The next two digits stands for city part, here including 01 and 02. The last two represent districts.
Here we need to grep the first two digits, and let them be equal to 11:
Now we generate some random numbers to show the diagram.
Then we can draw the map:
The package of sp give us an option of calculating the coordinate (longitude and latitude) of a map polygon:
Then we can add text annotation to our previous beijing plot:
Here is the plot:
One place, multiple polygons
Here we take Shanghai as the example. The preprocess of data is as previous:
Next post, I will talk about another powerful package – ggmap.