【问题标题】:How to use multiple columns as the frame of an animation in gganimate package (R)?如何在 gganimate 包(R)中使用多列作为动画的框架?
【发布时间】:2018-08-21 04:52:02
【问题描述】:

我正在尝试使用 R 的 gganimate 包来创建一堆直方图的动画,其中动画的每一帧都显示图像的直方图。我有 ~400 张图片,所以 ~400 列。我的数据如下所示:

| bins.left | bins.right | hist1 | hist 2 | ... | hist n |

如您所见,我需要将每一列视为直方图的 Y 值,在每一帧中。换句话说,我的动画应该遍历列。

但我在互联网上研究的所有示例似乎都只考虑一列作为框架的标识符。比如this example:

mapping <- aes(x = gdpPercap, y = lifeExp, 
           size = pop, color = continent,
           frame = year) 
p <- ggplot(gapminder, mapping = mapping) +
  geom_point() +
  scale_x_log10()

属性'Year'被认为是迭代器。该数据如下所示:

  country continent  year lifeExp      pop gdpPercap
   <fctr>    <fctr> <int>   <dbl>    <int>     <dbl>
1 Afghanistan      Asia  1952  28.801  8425333  779.4453
2 Afghanistan      Asia  1957  30.332  9240934  820.8530
3 Afghanistan      Asia  1962  31.997 10267083  853.1007
4 Afghanistan      Asia  1967  34.020 11537966  836.1971
5 Afghanistan      Asia  1972  36.088 13079460  739.9811
6 Afghanistan      Asia  1977  38.438 14880372  786.1134

我不想修改我的数据以适应这种模式的原因是,如果我将所有直方图保存在一列中,我的数据框将非常长(长度 = ~ 16000 * 400)并且很难处理。此外,以如此混乱的方式保存我的数据并不直观。我相信我的问题必须有一个简单的解决方案。任何建议都非常感谢。

【问题讨论】:

  • 你说你有大约 400 张图片。您的意思是要转换为直方图的 400 个数据帧吗?请使用dput 发布您的数据的实际样本(例如,将dput(my_data[1:10, ]) 的输出粘贴到您的问题中。
  • R 中的大多数工具都假定您的数据是 long 而不是 wide,因此数据通常更容易工作长格式。无论哪种方式,您的单元格总数都相同,所以我不确定我是否理解您对重塑数据的担忧。
  • @Marius,这种形式的数据不容易查看,避免一些潜在的错误。将时间序列分别放在单独的列中似乎很自然。我不想仅仅因为我不知道如何让它在 gganimate 中工作而重塑我的数据。但如果不可能,那我就得重新塑造它。

标签: r animation ggplot2 visualization gganimate


【解决方案1】:

正如@Marius 所说,如果您的数据是长格式,您可以完成这项工作。下面我创建一些假数据,然后制作动画情节。

library(tidyverse)
theme_set(theme_classic())
library(gganimate)

这是我们想要转换成直方图的 10 列值的假数据。

set.seed(2)
dat = replicate(10, runif(100)) %>% as.data.frame

数据是宽格式的,所以我们先用gather函数把它转换成长格式:

d = dat %>% gather(key, value)

在新的长格式中,key 列告诉我们数据最初来自哪个直方图列。我们将使用它作为我们的frame 并运行geom_histogram

p = ggplot(d, aes(value, frame=key)) +
  geom_histogram() 

gganimate(p)

你可以看到这不是我们想要的。 ggplot 实际上从所有数据中生成了一个直方图,而动画只是依次向我们展示了每个堆栈中来自 key 的每个值的部分。

我们需要一种方法来让 ggplot 创建单独的直方图并为其设置动画。我们可以通过预先分箱数据并使用geom_rect 创建直方图条来做到这一点:

d = dat %>% gather(key, value) %>% 
  mutate(bins = cut(value, breaks=seq(0,1,0.1), 
                    labels=seq(0,0.9,0.1) + 0.05, include.lowest=TRUE),
         bins = as.numeric(as.character(bins))) %>% 
  group_by(key, bins) %>% 
  tally

p = ggplot(d, aes(xmin=bins - 0.048, xmax=bins + 0.048, ymin=0, ymax=n, frame=key)) +
  geom_rect() +
  scale_y_continuous(limits=c(0, max(d$n)))

gganimate(p)

针对您的评论,我认为您不能将gganimate 用于宽格式数据。 gganimate 需要一个 frame 列,这需要长格式的数据。但是,gganimateanimation 包的包装,您可以使用 for 循环和 saveGIF 函数直接创建动画 GIF 文件:

library(animation)

saveGIF({
  for (i in names(dat)) {
    p = ggplot(dat, aes_string(i)) +
      geom_histogram(breaks=seq(0,1,0.1))
    print(p)
  }
}, movie.name="test.gif")

【讨论】:

  • eipi10,非常感谢。这绝对解决了我的问题。在我将此标记为答案之前,请允许我稍等片刻,看看是否有人可以提出一种可行的方法,而无需将所有时间序列首尾相连地放在一列中。再次感谢。
  • 查看我的答案的更新,了解一种不同的方法,可以让您以宽格式保存数据。
  • 感谢您的详尽回答。
猜你喜欢
  • 2020-02-02
  • 2020-02-22
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 1970-01-01
相关资源
最近更新 更多