R語言使用gganimate創(chuàng)建可視化動(dòng)圖
前言
介紹一個(gè)主要用于繪制動(dòng)畫的ggplot2的擴(kuò)展包---gganimate包。
Hans Rosling的關(guān)于“New Insights on Poverty”的TED演講絕對(duì)是對(duì)我影響最大的幾個(gè)TED之一,原來數(shù)據(jù)可以這樣展示,,,可視化可以這樣炫,,,故事可以這樣講...

下面嘗試使用 gganimate 包和 gapminder 數(shù)據(jù)集,實(shí)現(xiàn)類似可視化過程。
加載R包,數(shù)據(jù)
#R包安裝
install.packages("devtools")
library(devtools)
install_github("thomasp85/gganimate")
install.packages("gapminder")
#加載
library(gganimate)
library(gapminder)
#查看數(shù)據(jù)
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.數(shù)據(jù)集包括全球主要國(guó)家在1952-2007年的人均GDP增長(zhǎng)、預(yù)期壽命以及人口增長(zhǎng)的數(shù)據(jù) 。
ggplot2繪制
使用ggplot2繪制
theme_set(theme_bw()) p <- ggplot(gapminder, aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)) + geom_point(show.legend = FALSE, alpha = 0.7) + scale_color_viridis_d() + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy") p

gganimate 動(dòng)態(tài)
1. transition_time() 核心函數(shù),添加動(dòng)態(tài)
p + transition_time(year) +
labs(title = "Year: {frame_time}")
2 按需設(shè)置
1)添加小尾巴
p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_wake(wake_length = 0.1, alpha = FALSE)
2)原數(shù)據(jù)做背景
p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_mark(alpha = 0.3, size = 0.5)
參考資料
https://www.datanovia.com/en/blog/gganimate-how-to-create-plots-with-beautiful-animation-in-r/
https://www.ted.com/talks/hans_rosling_the_best_stats_you_ve_ever_seen
以上就是R語言使用gganimate創(chuàng)建可視化動(dòng)圖的詳細(xì)內(nèi)容,更多關(guān)于gganimate可視化動(dòng)圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
R語言數(shù)據(jù)可視化繪圖bar chart條形圖實(shí)現(xiàn)示例
這篇文章主要為大家介紹了R語言數(shù)據(jù)可視化繪圖bar chart條形圖的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
在R語言中實(shí)現(xiàn)Logistic邏輯回歸的操作
這篇文章主要介紹了在R語言中實(shí)現(xiàn)Logistic邏輯回歸的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
R語言中其它對(duì)象知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于R語言中其它對(duì)象知識(shí)點(diǎn)總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-03-03
R語言-計(jì)算平均值不同函數(shù)的區(qū)別說明
這篇文章主要介紹了R語言-計(jì)算平均值不同函數(shù)的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
R語言初學(xué)者的一些常見報(bào)錯(cuò)指南
在使用R的時(shí)候,偶爾也會(huì)遇到一些糟心的報(bào)錯(cuò),本文把運(yùn)行過程中遇到的一些報(bào)錯(cuò)和解決方案進(jìn)行總結(jié)歸納,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08

