R语言使用gganimate创建可视化动图
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>前言</li><li>加载R包,数据</li><li>ggplot2绘制</li><li>gganimate 动态</li><li>参考资料</li></ul></div><p class="maodian"></p><h2>前言</h2><p>介绍一个主要用于绘制动画的ggplot2的扩展包---gganimate包。</p>
<p>Hans Rosling的关于“New Insights on Poverty”的TED演讲绝对是对我影响最大的几个TED之一,原来数据可以这样展示,,,可视化可以这样炫,,,故事可以这样讲...</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202205/2022051208292901.jpg" /></p>
<p>下面尝试使用 gganimate 包和 gapminder 数据集,实现类似可视化过程。</p>
<p class="maodian"></p><h2>加载R包,数据</h2>
<div class="jb51code"><pre class="brush:ruby;">#R包安装
install.packages("devtools")
library(devtools)
install_github("thomasp85/gganimate")
install.packages("gapminder")
#加载
library(gganimate)
library(gapminder)
#查看数据
head(gapminder)
# A tibble: 6 x 6
country continentyear lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 28.88425333 779.
2 Afghanistan Asia 1957 30.39240934 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.</pre></div>
<p>数据集包括全球主要国家在1952-2007年的人均GDP增长、预期寿命以及人口增长的数据 。</p>
<p class="maodian"></p><h2>ggplot2绘制</h2>
<p>使用ggplot2绘制</p>
<div class="jb51code"><pre class="brush:ruby;">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</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202205/2022051208292902.jpg" /></p>
<p class="maodian"></p><h2>gganimate 动态</h2>
<p>1. <code>transition_time()</code> 核心函数,添加动态</p>
<div class="jb51code"><pre class="brush:ruby;">p + transition_time(year) +
labs(title = "Year: {frame_time}")</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202205/2022051208292903.gif" /></p>
<p>2 按需设置</p>
<p>1)添加小尾巴</p>
<div class="jb51code"><pre class="brush:ruby;">p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_wake(wake_length = 0.1, alpha = FALSE)</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202205/2022051208292904.gif" /></p>
<p>2)原数据做背景</p>
<div class="jb51code"><pre class="brush:ruby;">p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_mark(alpha = 0.3, size = 0.5)</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202205/2022051208292905.gif" /></p>
<p class="maodian"></p><h2>参考资料</h2>
<p>https://www.datanovia.com/en/blog/gganimate-how-to-create-plots-with-beautiful-animation-in-r/</p>
<p>https://www.ted.com/talks/hans_rosling_the_best_stats_you_ve_ever_seen</p>
<p>以上就是R语言使用gganimate创建可视化动图的详细内容,更多关于gganimate可视化动图的资料请关注琼殿技术社区其它相关文章!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>R语言编程学习绘制动态图实现示例</li><li>R语言基本画图函数与多图多线的用法</li><li>R语言绘图样式设置操作(符号,线条,颜色,文本属性)</li><li>R语言绘制Vonoroi图的完整代码</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]