gja*_*bel 6 animation r tween ggplot2 gganimate
我正在尝试从已经运行的数据集中添加年份标题tweenr.以来来自revolutionanalytics.com的示例
library(tidyverse)
library(tweenr)
library(gapminder)
gapminder_edit <- gapminder %>%
arrange(country, year) %>%
select(gdpPercap,lifeExp,year,country, continent, pop) %>%
rename(x=gdpPercap,y=lifeExp,time=year,id=country) %>%
mutate(ease="linear")
gapminder_tween <- tween_elements(gapminder_edit,
"time", "id", "ease", nframes = 150) %>%
mutate(year = round(time), country = .group) %>%
left_join(gapminder, by=c("country","year","continent")) %>%
rename(population = pop.x)
gapminder_tween %>% arrange(country, .frame) %>% head()
# x y time continent population .frame .group year country lifeExp pop.y gdpPercap
# 1 779.4453 28.80100 1952.000 Asia 8425333 0 Afghanistan 1952 Afghanistan 28.801 8425333 779.4453
# 2 781.7457 28.88606 1952.278 Asia 8470644 1 Afghanistan 1952 Afghanistan 28.801 8425333 779.4453
# 3 784.0462 28.97111 1952.556 Asia 8515955 2 Afghanistan 1953 Afghanistan NA NA NA
# 4 786.3466 29.05617 1952.833 Asia 8561267 3 Afghanistan 1953 Afghanistan NA NA NA
# 5 788.6470 29.14122 1953.111 Asia 8606578 4 Afghanistan 1953 Afghanistan NA NA NA
# 6 790.9475 29.22628 1953.389 Asia 8651889 5 Afghanistan 1953 Afghanistan NA NA NA
Run Code Online (Sandbox Code Playgroud)
要创建gif,我可以使用框架标题(有点无意义)并title_frame = TRUE在gganimate函数中设置(默认).
library(gganimate)
library(animation)
p2 <- ggplot(gapminder_tween,
aes(x=x, y=y, frame = .frame)) +
geom_point(aes(size=population, color=continent),alpha=0.8) +
xlab("GDP per capita") +
ylab("Life expectancy at birth") +
scale_x_log10()
magickPath <- shortPathName("C:\\Program Files\\ImageMagick-7.0.6-Q16\\magick.exe")
gganimate(p2, ani.options = ani.options(convert=magickPath), interval = 0.1)
Run Code Online (Sandbox Code Playgroud)
我尝试使用年份列(frame = year在映射美学中),但这只产生56帧,每帧出现多次点.
p2 <- ggplot(gapminder_tween,
aes(x=x, y=y, frame = year)) +
geom_point(aes(size=population, color=continent),alpha=0.8) +
xlab("GDP per capita") +
ylab("Life expectancy at birth") +
scale_x_log10()
Run Code Online (Sandbox Code Playgroud)
我是否(如果是这样,如何)拥有第一个带有标题的gif,每个帧对应year于补间数据帧中的相应值?
我修改了该gg_animate功能,引入了使用美学自定义情节标题的可能性ttl。在此处
下载文件并将其保存在您的工作目录中,名称为.
然后,运行以下代码:mygg_animate.r
library(tidyverse)
library(tweenr)
library(gapminder)
gapminder_edit <- gapminder %>%
arrange(country, year) %>%
select(gdpPercap,lifeExp,year,country, continent, pop) %>%
rename(x=gdpPercap,y=lifeExp,time=year,id=country) %>%
mutate(ease="linear")
gapminder_tween <- tween_elements(gapminder_edit,
"time", "id", "ease", nframes = 200) %>%
mutate(year = round(time), country = .group) %>%
left_join(gapminder, by=c("country","year","continent")) %>%
rename(population = pop.x)
library(gganimate)
library(animation)
source("mygg_animate.r")
# Define plot titles using the new aesthetic
p2 <- ggplot(gapminder_tween,
aes(x=x, y=y, frame=.frame, ttl=year)) +
geom_point(aes(size=population, color=continent),alpha=0.8) +
xlab("GDP per capita") +
ylab("Life expectancy at birth") +
scale_x_log10()
magickPath <- shortPathName("C:\\Program Files\\ImageMagick-7.0.6-Q16\\magick.exe")
mygg_animate(p2, ani.options = ani.options(convert=magickPath),
interval = 0.1, title_frame=T)
Run Code Online (Sandbox Code Playgroud)
生成的动画图下方(为了减小 gif 文件的尺寸,时间序列已被截断)。
| 归档时间: |
|
| 查看次数: |
562 次 |
| 最近记录: |