本文主要展示在R语言中如何利用ggplot2和gganimate包绘制分面散点图及分面动态散点图[1]。绘图效果如下:
1、数据
使用gapminder包中的关于全球一百多个国家或地区的关于预期寿命(lifeExp)、人口数量(pop)、人均GDP(gdpPercap)的面板数据为例。
#install.packages("RColorBrewer")
#install.packages("gapminder")
#install.packages("gganimate")
#install.packages("ggplot2")
library(RColorBrewer)
library(gapminder)
library(gganimate)
library(ggplot2)
head(gapminder)
# A tibble: 6 x 6
# country continent year lifeExp pop gdpPercap
# <fct> <fct> <int> <dbl> <int> <dbl>
#1 Afghanistan Asia 1952 28.8 8425333 779.
#2 Afghanistan Asia 1957 30.3 9240934 821.
#3 Afghanistan Asia 1962 32.0 10267083 853.
#4 Afghanistan Asia 1967 34.0 11537966 836.
#5 Afghanistan Asia 1972 36.1 13079460 740.
#6 Afghanistan Asia 1977 38.4 14880372 786.
> str(gapminder)
#tibble [1,704 x 6] (S3: tbl_df/tbl/data.frame)
# $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
# $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
# $ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
# $ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ...
# $ pop : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
# $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
2、分面散点图绘制
在分面图绘制的过程中主要使用三类函数或参数,一类是分面函数,包括facet_grid() 和facet_wrap() ,二者效果可视数据的情况进行选择;另一类是控制坐标轴的数值函数,包括scales = "free",scales = "free_x",scales = "free_y" ;还一类为控制分面标签文本大小的函数,包括strip.text,strip.text.x,strip.text.y三个参数,分别控制整体分面标签和x轴、y轴分面标签。
使用facet_wrap() 函数进行分面
ggplot(data = gapminder ,aes(x =gdpPercap, y = lifeExp, fill = continent )) +
geom_point(shape = 21, color ="black", size = 3) +
scale_fill_brewer(palette = "Set3") +
facet_wrap(~ continent) +
theme_bw()
使用ncol、scales、strip.text绘图参数调整图形。
ggplot(data = gapminder ,aes(x = gdpPercap, y = lifeExp, fill = continent )) +
geom_point(shape = 21, color ="black", size = 3) +
scale_fill_brewer(palette = "Set3") +
facet_wrap(~ year,ncol = 4, scales = "free" ) +
theme_bw() +
theme(strip.text = element_text(size = 20))

使用facet_grid() 函数进行分面
ggplot(data = gapminder ,aes(x = gdpPercap, y = lifeExp, fill = continent )) +
geom_point(shape = 21, color ="black", size = 3) +
scale_fill_brewer(palette = "Set3") +
facet_grid(year ~ continent) +
theme_bw()
使用scales绘图参数调整图形。
ggplot(data = gapminder ,aes(x = gdpPercap, y = lifeExp, fill = continent )) +
geom_point(shape = 21, color ="black", size = 3) +
scale_fill_brewer(palette = "Set3") +
facet_grid(continent ~ year, scales = "free") +
theme_bw()

3、动态分面散点图绘制
ggplot(data = gapminder ,aes(x =gdpPercap, y = lifeExp, fill = continent )) +
geom_point(shape = 21, color ="black", size = 5) +
scale_fill_brewer(palette = "Set3") +
facet_wrap(~ continent, scales = "free", ncol = 1 ) + #scales = "free_y"
theme_bw() +
labs(title = 'Year: {frame_time}', x = 'Per.GDP', y = 'lifeExp') +
transition_time(year)
anim_save("facet_gapminder2.gif")
加入shadow_wake() 绘图函数显示点的运动轨迹。
ggplot(data = gapminder ,aes(x =gdpPercap, y = lifeExp, fill = continent )) +
geom_point(shape = 21, color ="black", size = 5) +
scale_fill_brewer(palette = "Set3") +
facet_wrap(~ continent, scales = "free", ncol = 1 ) + #scales = "free_y"
theme_bw() +
labs(title = 'Year: {frame_time}', x = 'Per.GDP', y = 'lifeExp') +
transition_time(year) +
shadow_wake(wake_length = 0.5)
anim_save("facet_gapminder3.gif")
4、其他
关于gganimate[2]包的更多内容可阅读参考文档。关于动态散点图的绘制可参考公众号前期推文。关于动态折线图的绘制可参考R语言绘图|动态折线图。更多绘图方法可阅读公众号其他推文。
如有帮助请多多点赞哦!
参考资料
Animated bubble chart with R and gganimate: https://r-graph-gallery.com/271-ggplot2-animated-gif-chart-with-gganimate.html
[2]gganimate: https://cran.r-project.org/web/packages/gganimate/gganimate.pdf




