【发布时间】:2023-03-13 00:33:01
【问题描述】:
我创建了两个基于同一数据集的动画时间序列图表。一个是显示总体趋势的折线图,一个是显示前 5 个子组的条形图。我想看看我是否可以将它们组合成一个时间序列动画,可以并排或与折线图一起作为小插图。
我已经构建了单独的动画,但我不清楚是否可以将它们组合起来,或者我是否需要创建一个新图像,每个帧都有插图,然后对其进行动画处理。
这是一个最小的示例数据集:
data <- data.frame(
Year = c(rep(1988, 5), rep(1989, 5), rep(1990, 5)),
Total = c(rep(1000, 5), rep(1100, 5), rep(1200, 5),
SubGroup = c("A", "B", "C", "D", "E", "B", "A", "C", "D", E", "B", "C", "A", "E", "D"),
Amount = c(200, 180, 100, 80, 60, 210, 200, 150, 100, 80, 250, 240, 200, 150, 110)
)
这是我的前 5 个条形图动画的代码:
# generate top 5 ranking by year
anim_table <- data %>%
dplyr::group_by(Year) %>%
dplyr::mutate(
rank = min_rank(-Amount) * 1,
Value_rel = Amount / Amount[rank == 1],
Value_lbl = paste0(" ", Amount)
) %>%
dplyr::filter(rank <= 5) %>%
dplyr::ungroup() %>%
dplyr::arrange(Year, rank)
# create static barchart
p1 <- ggplot2::ggplot(anim_table, aes(rank)) +
ggplot2::geom_tile(aes(
y = Amount / 2,
height = Amount,
width = 0.9,
fill = "blue"
), alpha = 0.8, color = NA) +
ggplot2::geom_text(aes(y = 0, label = paste(SubGroup, " ")), size = 12, vjust = 0.2, hjust = 1) +
ggplot2::geom_text(aes(y = Amount, label = Value_lbl, hjust = 0)) +
ggplot2::coord_flip(clip = "off", expand = FALSE) +
ggplot2::scale_y_continuous(labels = scales::comma) +
ggplot2::scale_x_reverse() +
ggplot2::guides(color = FALSE, fill = FALSE) +
ggplot2::labs(
title = "{closest_state}", x = "", y = "Amount",
caption = "Source: whatever"
) +
ggplot2::theme(
plot.title = element_text(color = "darkblue", face = "bold", hjust = 0, size = 30),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
plot.margin = margin(2, 2, 1, 16, "cm")
) +
# animate over Years
gganimate::transition_states(Year, transition_length = 4, state_length = 1) +
gganimate::ease_aes("cubic-in")
gganimate::animate(p1, 200, fps = 5, duration = 100, width = 2000, height = 1200, renderer = gifski_renderer("anim1.gif"))
这是我的简单动画折线图的代码:
p2 <- ggplot2::ggplot(
hc_total,
aes(Year, Total)
) +
ggplot2::geom_line() +
ggplot2::scale_color_viridis_d() +
ggplot2::labs(x = "Year", y = "Total") +
gganimate::transition_reveal(Year)
gganimate::animate(p2, 200, fps = 5, duration = 100, width = 2000, height = 1200, renderer = gifski_renderer("anim2.gif"))
正在研究如何将这两个动画并排或以折线图作为插图组合成一个动画。
【问题讨论】:
-
你可以用 ImageMagick 做到这一点。您使用的是 Windows 还是 Linux?
-
我在 Mac 上工作