## [1] "LC_COLLATE=Chinese (Simplified)_People's Republic of China.936;LC_CTYPE=Chinese (Simplified)_People's Republic of China.936;LC_MONETARY=Chinese (Simplified)_People's Republic of China.936;LC_NUMERIC=C;LC_TIME=Chinese (Simplified)_People's Republic of China.936"
Calculate the growth rate the private car from 2008 to 2013, assign the data to argument fill of geom_point().
1
2
3
4
5
6
7
8
9
10
11
cap_bubble["growth"] <- subset(car_prov, year == 2013)[,3,drop = F] /
subset(car_prov, year == 2008)[,3,drop = F]-1
ggplot() +
geom_polygon(data = cnmapdf, aes(long, lat, group = group),
fill = "skyblue", colour = "grey") +
geom_point(data = cap_bubble, shape = 21,
aes(cap_long, cap_lat, size = no_car_k,
fill = growth, alpha = .5)) +
scale_fill_gradient(low = "red", high = "yellow") +
scale_size_area(max_size=20)
The bubbles with transparency cannot display the difference clearly when the growth rates are similar across provinces. Let’s try smaller bubbles without specifying the transparency. We can also remove the legend to keep the map simple and clear.
1
2
3
4
5
6
7
8
ggplot() +
geom_polygon(data = cnmapdf, aes(long, lat, group = group),
layer_points(data = cap_bubble, x = ~cap_long, y = ~cap_lat,
fill = ~growth, size = ~no_car_k, stroke := "white") %>%
scale_numeric("fill", range = c("red", "yellow")) %>%
scale_numeric("size", range = c(50, 500)) %>%
hide_legend(scales = c("fill", "size")) %>%
add_axis("x", title = "Longitude") %>%
add_axis("y", title = "Latitude")
To remove the legend, ggvis provides hide_legend(), within this function, you can specify the names of scales to be hidden.
Bar Charts on A Map
Bar Charts by ggplot2
It is more complicated to place a bar chart than plot just a bubble on certain spot. To locate a bubble, you just need to give aes() the coordinates which have nothing to do with the size and color of bubble. Regarding bar chart, we need to assign at least 2 pairs of x and y, one for bar chart location and the other for the height and width of the bar chart. In other words, we need to know the coordinates of 4 corners for a bar.
Since a bar chart may takes more space and looks more complicated than a bubble, we just plot 4 cities.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
p1 <- ggplot() +
geom_polygon(data = cnmapdf, aes(long, lat, group = group),
layer_text(data = year_labels, x = ~long, y = ~lat,
text := ~label, stroke := ~fill) %>%
scale_nominal("fill", range = c("brown", "red", "orange")) %>%
layer_text(data = cap_bar, x = ~cap_long -.5, y = ~cap_lat - 1,
text := ~prov_en, stroke:= "black", align:="right") %>%
layer_text(data = cap_bar, x = ~cap_long +.5, y = ~cap_lat - 1,
text := ~round(growth_10y*100 ,2),
stroke:= "black", align:="left")
I tried to combine the last 2 layer_text() functions together (the chunk below) and add a “%” to the end of the percentage point but it returns undefined at the position which is supposed to be like Beijing 14.78%. Need to figure out why the function does not support multiple embedding.
1
2
3
4
layer_text(data = cap_bar, x = ~cap_long -.5, y = ~cap_lat - 1,
text := ~paste(prov_en, ": ", round(growth_10y * 100, 2),