gpt4 book ai didi

r - 将 gganimate 与 tweenr 一起使用时绘制标题

转载 作者:行者123 更新时间:2023-12-04 11:00:08 25 4
gpt4 key购买 nike

我正在尝试从已通过 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

要创建 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)

enter image description here

我尝试使用年份列(映射美学中的 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()

enter image description here

我可以(如果可以,如何)拥有第一个 gif,每个帧的标题对应于 year 的相应值吗?在补间数据框中?

最佳答案

我修改了gg_animate引入使用 ttl 自定义绘图标题的可能性的函数审美的。
下载文件 here并将其保存在您的工作目录中,名称为 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)

在生成的动画图下方(时间序列已被截断,以减少 gif 文件的维度)。

enter image description here

关于r - 将 gganimate 与 tweenr 一起使用时绘制标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45569659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com