我想从正态分布中采样点,然后使用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的Jaime Albella再现这张图表?
在visualcapitalist.com或twitter上查看动画(如果有一个中断,请提供几个参考).
我将其标记为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) 我想放大这些年来的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每一帧的比例?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) gganimate在gg_animate()调用渲染动画序列时可以设置浏览选项,似乎没有选项可以更改帧标题,以使观察者更清楚框架所基于的参数是什么.
换句话说,假设frame = year在一个图层中:如何将框架的标题设置为year: ########是当前帧的年份?我错过了什么或是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) 是否可以在gganimate循环之间添加暂停?我知道我们可以设置帧之间的间隔interval,但有没有办法在循环回到第一帧之前暂停最后一帧?
是将最终帧的多个副本插入数据的最佳方法吗?
利用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_length或transition_length参数可用,所以我不确定这是否可行.
编辑 …
我正在gganimate创建一些我想要插入到报告中的.gif文件.我能够保存文件并查看它们,但是,我发现显示的尺寸很小:480x480.有没有办法调整它 - 也许是沿着height和width参数的方式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)
以下是此代码的输出.
使用时,我想减慢状态之间的转换速度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) 该包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已更新并在新版本中,在查看器窗格中显示动画与导出它似乎是不同的问题.