标签: gganimate

动画点直方图,通过观察构建观察(在 R 中使用 gganimate)

我想从正态分布中采样点,然后使用gganimate包一个一个地建立一个点图,直到最后一帧显示完整的点图。

适用于约 5,000 - 20,000 个点的更大数据集的解决方案至关重要。

这是我到目前为止的代码:

library(gganimate)
library(tidyverse)

# Generate 100 normal data points, along an index for each sample 
samples <- rnorm(100)
index <- seq(1:length(samples))

# Put data into a data frame
df <- tibble(value=samples, index=index)
Run Code Online (Sandbox Code Playgroud)

df 看起来像这样:

> head(df)
# A tibble: 6 x 2
    value index
    <dbl> <int>
1  0.0818     1
2 -0.311      2
3 -0.966      3
4 -0.615      4
5  0.388      5
6 -1.66       6
Run Code Online (Sandbox Code Playgroud)

静态图显示了正确的点图:

# Create static version
plot <- ggplot(data=df, …
Run Code Online (Sandbox Code Playgroud)

r data-visualization ggplot2 gganimate

20
推荐指数
3
解决办法
787
查看次数

动画排序的条形图与酒吧互相超越

你如何从R的Jaime Albella再现这张图表?

visualcapitalist.comtwitter上查看动画(如果有一个中断,请提供几个参考).

在此输入图像描述

我将其标记为ggplot2,gganimate但是可以从R生成的任何内容都是相关的.

数据(感谢https://github.com/datasets/gdp)

gdp <- read.csv("https://raw.github.com/datasets/gdp/master/data/gdp.csv")
# remove irrelevant aggregated values
words <- scan(
  text="world income only total dividend asia euro america africa oecd",
  what= character())
pattern <- paste0("(",words,")",collapse="|")
gdp  <- subset(gdp, !grepl(pattern, Country.Name , ignore.case = TRUE))
Run Code Online (Sandbox Code Playgroud)

animation r data-visualization ggplot2 gganimate

18
推荐指数
3
解决办法
6344
查看次数

通过gganimate和ggforce移动的facet缩放的动画情节?

在此输入图像描述

目标

我想放大这些年来的GDP Europe.幻象ggforce::facet_zoom允许非常容易地将其用于静态图(即,特定年份).

然而,移动尺度证明比预期更难.gganimate似乎从第一帧(year == 1952)获取x轴限制并一直持续到动画结束.不幸的是,这个相关但代码过时的问题没有得出答案.既不能+ coord_cartesian(xlim = c(from, to)),也facet_zoom(xlim = c(from, to))不能影响facet_zoom超出静态极限的窗口.

  • 有没有办法让gganimate'重新计算' facet_zoom每一帧的比例?

理想的结果

第一帧

2

最后一帧

3

目前的代码
library(gapminder)
library(ggplot2)
library(gganimate)
library(ggforce)
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent)) +
    geom_point() + scale_x_log10() +
    facet_zoom(x = continent == "Europe") +
    labs(title = "{frame_time}") +
    transition_time(year) 

animate(p, nframes = 30)
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate ggforce

17
推荐指数
1
解决办法
950
查看次数

更改gigimate框架标题的标签

gganimategg_animate()调用渲染动画序列时可以设置浏览选项,似乎没有选项可以更改帧标题,以使观察者更清楚框架所基于的参数是什么.

换句话说,假设frame = year在一个图层中:如何将框架的标题设置为year: ########是当前帧的年份?我错过了什么或是gganimate图书馆的限制吗?

您将如何通过变通方法获得相同的结果?谢谢你的建议.

animation r ggplot2 gganimate

16
推荐指数
1
解决办法
3510
查看次数

如何使用 rmarkdown::render() 在 html 中渲染 gganimate 图形,而不会产生不需要的输出

我正在尝试使用 r-markdown 文档在 html 中呈现 gganimate() 图。我能够创建 html 文档(尽管下面的简单示例至少需要一分钟)并且 gganimate 图形在浏览器(firefox)中成功加载,但是,我在浏览器中得到了一堆不需要的输出。

不需要的输出如下所示:


第 1 帧 (1%)

第 2 帧 (2%)

第 3 帧 (3%)

...

第 96 帧 (96%)

第 97 帧 (97%)

帧 98 (98%)

帧 99 (99%)

帧 100 (100%)

完成编码...完成!


就像我说的,在这个不需要的输出之后,动画确实显示正确。

我尝试过使用 knitR 代码块标题选项。主要是在这里查看这些选项:https : //bookdown.org/yihui/rmarkdown/r-code.html

我也尝试了这篇文章中建议的解决方案:在 r markdown 中抑制控制台输出,但保留情节具体来说,我尝试将 ggplot 对象包装在“不可见”中。

只需将下面的代码复制到 Rmarkdown 文档中,将此 Rmarkdown 文档保存为“example.Rmd”,然后在 R 控制台中运行: rmarkdown::render("example.Rmd")

---
    title: "Testing gganimate with R Markdown"
    output: html_document
---

```{r message = FALSE} …
Run Code Online (Sandbox Code Playgroud)

r knitr r-markdown gganimate

13
推荐指数
1
解决办法
3926
查看次数

在gganimate循环之间暂停

是否可以在gganimate循环之间添加暂停?我知道我们可以设置帧之间的间隔interval,但有没有办法在循环回到第一帧之前暂停最后一帧?

是将最终帧的多个副本插入数据的最佳方法吗?

visualization r ggplot2 gganimate

12
推荐指数
2
解决办法
1953
查看次数

使用transition_reveal在gganimate中以特定帧/时间点暂停的任何方法?

利用Github上包的wiki中的这个例子:

airq <- airquality
airq$Month <- format(ISOdate(2004,1:12,1),"%B")[airq$Month]

ggplot(airq, aes(Day, Temp, group = Month)) + 
  geom_line() + 
  geom_segment(aes(xend = 31, yend = Temp), linetype = 2, colour = 'grey') + 
  geom_point(size = 2) + 
  geom_text(aes(x = 31.1, label = Month), hjust = 0) + 
  transition_reveal(Month, Day) + 
  coord_cartesian(clip = 'off') + 
  labs(title = 'Temperature in New York', y = 'Temperature (°F)') + 
  theme_minimal() + 
  theme(plot.margin = margin(5.5, 40, 5.5, 5.5))
Run Code Online (Sandbox Code Playgroud)

产生类似的东西:

在此输入图像描述

我想知道是否有任何方法可以在特定点定义动画中的暂停.例如,在第10天,然后是20,然后在动画结束时,再次循环之前.geom_reveal没有state_lengthtransition_length参数可用,所以我不确定这是否可行.

编辑 …

r gganimate

12
推荐指数
1
解决办法
1335
查看次数

定义由gganimate创建的.gif的大小

我正在gganimate创建一些我想要插入到报告中的.gif文件.我能够保存文件并查看它们,但是,我发现显示的尺寸很小:480x480.有没有办法调整它 - 也许是沿着heightwidth参数的方式ggsave()

我可以放大,但这会影响质量,并使我的用例难以理解.

这是一些示例代码:

gplot<- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, colour = continent, 
               size = pop, frame = year)) +
        geom_point(alpha = 0.6) + scale_x_log10()

gganimate(gplot, "test.gif")
Run Code Online (Sandbox Code Playgroud)

以下是此代码的输出.

test.gif

r ggplot2 gganimate

11
推荐指数
3
解决办法
5198
查看次数

动画的控制速度

使用时,我想减慢状态之间的转换速度library(gganimate)

这是一个迷你示例:

# devtools::install_github("thomasp85/gganimate")
library(gganimate) # v0.9.9.9999

dat_sim <- function(t_state, d_state) {
  data.frame(
    x = runif(1000, 0, 1),
    y = runif(1000, 0, 1),
    t_state = t_state*d_state
    )
}

dat <- purrr::map_df(1:100, ~ dat_sim(., 1))

ggplot(dat, aes(x, y)) +
  geom_hex(bins = 5) +
  theme_void() +
  lims(x = c(.3, .7),
       y = c(.3, .7)) +
  theme(legend.position = "none") +
  transition_time(t_state)
Run Code Online (Sandbox Code Playgroud)

太快

我的理想行为会慢很多(10-100倍),因此颜色会逐渐演变,没有人会发作。

如果我尝试使用transition_states()更多的手动控制,则会得到带有大部分空白帧的gif。我已经尝试了各种组合transition_legnth=state_length=但效果并不明显。

ggplot(dat, aes(x, y)) +
  geom_hex(bins = 5) +
  theme_void() +
  lims(x = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

11
推荐指数
1
解决办法
2550
查看次数

使用gganimate导出gif

该包gganimate创建了gifs(MWE代码来自这里):

    library(ggplot2)
    #devtools::install_github('thomasp85/gganimate')
    library(gganimate)

    p <- ggplot(mtcars, aes(factor(cyl), mpg)) + 
            geom_boxplot() + 
            # Here comes the gganimate code
            transition_states(
                    gear,
                    transition_length = 2,
                    state_length = 1
            ) +
            enter_fade() + 
            exit_shrink() +
            ease_aes('sine-in-out')
Run Code Online (Sandbox Code Playgroud)

现在如何导出这个gif?在之前(现已存档)的版本中,gganimate这很简单:

    gganimate(p, "output.gif")
Run Code Online (Sandbox Code Playgroud)

但是,我在当前gganimate包中找不到等效函数.


注意:这个问题似乎与我从中获取MWE代码的问题完全重复.但是,gganimate已更新并在新版本中,在查看器窗格中显示动画与导出它似乎是不同的问题.

r ggplot2 gganimate

10
推荐指数
2
解决办法
4979
查看次数