如何使用 GGanimate 在数据帧列表之间制作动画?
How to use GGanimate to animate between a list of data frames?
我正在尝试绘制一组广告在一段时间内每天的支出和盈利数据图表。对于给定的日期范围,我遍历该范围并创建一个日期框架。这些 table 中的每一个都作为单独的数据帧附加到列表中。
下面是一些示例代码:
l <- list()
df1 <- tribble(
~Ad, ~Spend, ~Profitability,
"Cat", 10000, 0.21,
"Movie", 20000, -0.05,
"Dog", 8000, 0.07)
l[1] <- list(df1)
df2 <- tribble(
~Ad, ~Spend, ~Profitability,
"Cat", 14000, 0.25,
"Movie", 21000, -0.08,
"Dog", 8000, 0.09,
"TV", 4000, .31)
l[2] <- list(df2)
df3 <- tribble(
~Ad, ~Spend, ~Profitability,
"Cat", 13000, 0.18,
"Movie", 23000, -0.11,
"TV", 7000, .25)
l[3] <- list(df3)
到目前为止,我已经将每个 table 绘制成散点图(盈利能力作为 y 轴,支出作为 x 轴)并将它们保存为单独的 png 以将它们制作成 gif , 但看起来很起伏。
这是其中一个散点图的示例:
ggplot(as.data.frame(l[1]), aes(x = Spend, y = Profitability, color = Ad)) + geom_point(size = 3) + xlim(0, 30000) +
scale_y_continuous(labels = scales::percent, limits = c(-0.25, 0.5)) + geom_text(aes(label = Ad), size = 2, vjust = -2)
我的问题是,我如何制作此 gif 动画以使点每天连续移动(即 'Cat' 散点将从 (10000,0.21) 移动到 (14,000, 0.25) 到(13,000, 0.18)?
另一个复杂问题是广告集不一定每天都相同(table 到 table),因为有些广告在某些日子根本不花费。如果某处没有广告,我将相应的散点移动到 (0,0)。
谢谢!
library(purrr); library(gganimate)
l %>%
map_df(magrittr::extract, .id = "table",
c("Ad", "Spend", "Profitability")) %>%
complete(table, Ad, fill = list(Spend = 0, Profitability = 0)) %>%
ggplot(aes(Spend, Profitability, color = Ad)) +
geom_point() +
transition_states(table)
我正在尝试绘制一组广告在一段时间内每天的支出和盈利数据图表。对于给定的日期范围,我遍历该范围并创建一个日期框架。这些 table 中的每一个都作为单独的数据帧附加到列表中。
下面是一些示例代码:
l <- list()
df1 <- tribble(
~Ad, ~Spend, ~Profitability,
"Cat", 10000, 0.21,
"Movie", 20000, -0.05,
"Dog", 8000, 0.07)
l[1] <- list(df1)
df2 <- tribble(
~Ad, ~Spend, ~Profitability,
"Cat", 14000, 0.25,
"Movie", 21000, -0.08,
"Dog", 8000, 0.09,
"TV", 4000, .31)
l[2] <- list(df2)
df3 <- tribble(
~Ad, ~Spend, ~Profitability,
"Cat", 13000, 0.18,
"Movie", 23000, -0.11,
"TV", 7000, .25)
l[3] <- list(df3)
到目前为止,我已经将每个 table 绘制成散点图(盈利能力作为 y 轴,支出作为 x 轴)并将它们保存为单独的 png 以将它们制作成 gif , 但看起来很起伏。
这是其中一个散点图的示例:
ggplot(as.data.frame(l[1]), aes(x = Spend, y = Profitability, color = Ad)) + geom_point(size = 3) + xlim(0, 30000) +
scale_y_continuous(labels = scales::percent, limits = c(-0.25, 0.5)) + geom_text(aes(label = Ad), size = 2, vjust = -2)
我的问题是,我如何制作此 gif 动画以使点每天连续移动(即 'Cat' 散点将从 (10000,0.21) 移动到 (14,000, 0.25) 到(13,000, 0.18)?
另一个复杂问题是广告集不一定每天都相同(table 到 table),因为有些广告在某些日子根本不花费。如果某处没有广告,我将相应的散点移动到 (0,0)。
谢谢!
library(purrr); library(gganimate)
l %>%
map_df(magrittr::extract, .id = "table",
c("Ad", "Spend", "Profitability")) %>%
complete(table, Ad, fill = list(Spend = 0, Profitability = 0)) %>%
ggplot(aes(Spend, Profitability, color = Ad)) +
geom_point() +
transition_states(table)