美文网首页大数据,机器学习,人工智能大数据 爬虫Python AI Sql玩转大数据
R语言动态图可视化:如何、创建具有精美动画的图

R语言动态图可视化:如何、创建具有精美动画的图

作者: 拓端tecdat | 来源:发表于2020-04-01 23:27 被阅读0次

原文链接:http://tecdat.cn/?p=8003

演示数据集

library(gapminder) head(gapminder)

need-to-insert-img

## # A tibble: 6 x 6 ## country continent year lifeExp pop gdpPercap ## ## 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.

静态图

<- ggplot(\n gapminder, \n aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)\n ) +\n geom_point(show.legend = FALSE, alpha = 0.7) +\n scale_color_viridis_d() +\n scale_size(range = c(2, 12)) +\n scale_x_log10() +\n labs(x = \"GDP per capita\", y = \"Life expectancy\")\np","classes":{"has":1},"lang":""}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">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

need-to-insert-img

need-to-insert-img

基本

状态之间的过渡长度将设置为与它们之间的实际时间差相对应。

标签变量:frame_time。给出当前帧所对应的时间。

need-to-insert-img

创建面:

need-to-insert-img

让视图跟随数据在每帧中

need-to-insert-img

逐步衰减

need-to-insert-img

显示原始数据作为背景

您可以根据需要显示过去和/或将来的原始数据并设置其样式。

need-to-insert-img

静态图

<- ggplot(\n airquality,\n aes(Day, Temp, group = Month, color = factor(Month))\n ) +\n geom_line() +\n scale_color_viridis_d() +\n labs(x = \"Day of Month\", y = \"Temperature\") +\n theme(legend.position = \"top\")\np","classes":{"has":1},"lang":""}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">p <- ggplot( airquality, aes(Day, Temp, group = Month, color = factor(Month)) ) + geom_line() + scale_color_viridis_d() + labs(x = "Day of Month", y = "Temperature") + theme(legend.position = "top") p

need-to-insert-img

need-to-insert-img

让数据逐渐出现

按天显示(x轴)

need-to-insert-img

need-to-insert-img

need-to-insert-img

在数据的几个不同阶段之间进行转换

数据准备:

<- airquality %>%\n group_by(Month) %>%\n summarise(Temp = mean(Temp))\nmean.temp","classes":{"has":1},"lang":""}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">library(dplyr) mean.temp <- airquality %>% group_by(Month) %>% summarise(Temp = mean(Temp)) mean.temp

need-to-insert-img

## # A tibble: 5 x 2 ## Month Temp ## ## 1 5 65.5 ## 2 6 79.1 ## 3 7 83.9 ## 4 8 84.0 ## 5 9 76.9

创建平均温度的条形图:

<- ggplot(mean.temp, aes(Month, Temp, fill = Temp)) +\n geom_col() +\n scale_fill_distiller(palette = \"Reds\", direction = 1) +\n theme_minimal() +\n theme(\n panel.grid = element_blank(),\n panel.grid.major.y = element_line(color = \"white\"),\n panel.ontop = TRUE\n )\np","classes":{"has":1},"lang":""}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">p <- ggplot(mean.temp, aes(Month, Temp, fill = Temp)) + geom_col() + scale_fill_distiller(palette = "Reds", direction = 1) + theme_minimal() + theme( panel.grid = element_blank(), panel.grid.major.y = element_line(color = "white"), panel.ontop = TRUE ) p

need-to-insert-img

need-to-insert-img

transition_states():

need-to-insert-img

enter_grow()+ enter_fade()

保存动画

如果需要保存动画以备后用,可以使用该anim_save()功能。

相关文章

网友评论

    本文标题:R语言动态图可视化:如何、创建具有精美动画的图

    本文链接:https://www.haomeiwen.com/subject/vjsruhtx.html