如何创建一个动画:1.在与测量时间成比例的时间向绘图添加新点,以及2.逐渐消除旧点?
我想也许我可以通过以下代码实现这一目标:
library(tidyverse)
library(gganimate)
set.seed(1)
ex =
expand.grid(x = seq(0, 1, 0.2),
y = seq(0, 1, 0.2),
t = seq(0, 10, 1)) %>%
as_tibble() %>%
mutate(z = rnorm(x, mean = t, sd = 0.2))
ggplot(data = ex,
mapping = aes(x, y, color = z)) +
geom_jitter(position = position_jitter(width = 0.02, height = 0.02)) +
transition_time(t) +
scale_color_viridis_c() +
shadow_mark(alpha = 0.4, size = 1)
Run Code Online (Sandbox Code Playgroud)
但是,此图会在时间内插入点的位置.虽然这对于再现gapminder是有意义的,但它似乎并不是我正在寻找的效果:我想要同样的东西,但没有移动的球.我只是希望它们出现然后淡出.但我想知道使用语法是否有一个很好的方法来做到这一点?
我想将我的数据的另一列值插入到gganimate动画标题中.
例如,这里的状态级变量是x,我想添加到标题变量y:
df <- tibble(x = 1:10, y = c('a', 'a', 'b', 'd', 'c', letters[1:5]))
df
A tibble: 10 x 2
x y
<int> <chr>
1 1 a
2 2 a
3 3 b
4 4 d
5 5 c
6 6 a
7 7 b
8 8 c
9 9 d
10 10 e
Run Code Online (Sandbox Code Playgroud)
这按预期工作:
ggplot(df, aes(x, x)) +
geom_point() +
labs(title = '{closest_state}') +
transition_states(x,
transition_length = 0.1,
state_length = …Run Code Online (Sandbox Code Playgroud) 我已经解析了一些来自电子游戏反恐精英的手榴弹投掷数据。下面的示例数据显示,我有关于手榴弹从何处投掷、手榴弹在何处引爆以及手榴弹何时投掷的位置。
df <- data.frame(pos_x = c(443.6699994744587,459.4566921116250, 443.5131582404877, 565.8823313012402, 725.3048665125078, 437.3428992800084, 475.7286794460795, 591.4138769182258),
pos_y = c(595.8564633895517, 469.8560006170301, 558.8543552036199, 390.5840189222542, 674.7983854380914, 688.0909476552858, 468.4987145207733, 264.6016042780749),
plot_group = c(1, 1, 2, 2, 3, 3, 4, 4),
round_throw_time = c(31.734375, 31.734375, 24.843750, 24.843750, 35.281250, 35.281250, 30.437500, 30.437500),
pos_type = c("Player position", "HE detonate", "Player position", "HE detonate", "Player position", "HE detonate", "Player position", "HE detonate"))
Run Code Online (Sandbox Code Playgroud)
但我想为手榴弹轨迹设置动画,并按照round_throw_time规定的顺序启动每个轨迹的动画,并从玩家位置移动到引爆位置。到目前为止,我已经尝试过这个:
ggplot(df, aes(pos_x, pos_y, group = plot_group)) +
annotation_custom(grid::rasterGrob(img, width = unit(1,"npc"), height =
unit(1,"npc")), …Run Code Online (Sandbox Code Playgroud) 使用gganimate从ggplot制作动画时,我需要设置较低的速度以允许人们读取数据。
阅读文档(很难找到选项)似乎是“ nframes”是正确的设置。但是我不能放慢动画或设置持续时间。两种方法都可以
library("gganimate")
library("tidyverse")
p <- ggplot(airquality, aes(Day, Temp, color = Month)) +
transition_time(Month) +
labs(title = 'Month is {frame_time}') +
geom_path(aes(group = Month), size = 1)
animate(p, nframes = 100)
Error in device(files[i], ...) : unused argument (nframes = 100)
Run Code Online (Sandbox Code Playgroud) 我正在使用 gganimate。假设我有这个 MWE:
library(ggplot2)
library(gganimate)
ggplot(airquality, aes(Day, Temp)) +
geom_point(color = 'red', size = 1) +
transition_time(Month) +
shadow_mark(colour = 'black', size = 0.75)
Run Code Online (Sandbox Code Playgroud)
我有一个问题:如何才能让新点出现而不是从旧点过渡?换句话说,我只想让新点出现在它们的最终位置并且没有过渡。我应该如何修改代码?
我正在处理空间数据。我想创建一个运动路径的动画,旧数据逐渐消失。我想出了如何用点创建淡出效果,但看起来没有对geom_path( Error: path layers not currently supported by transition_components) 过渡的内置支持。但是有什么聪明的解决方法可以使用吗?我的完整数据集很大(200K 点)并且重叠路径失去控制......
玩具数据:
library(ggplot2)
library(gganimate)
df <- structure(list(Lon = c(-66.6319163369509, -66.5400363369509,
-65.3972830036509, -65.2810430036509, -65.1169763369509, -64.7409730036509,
-64.3898230036509, -64.3458230036509, -64.1435830036509, -64.1902230036509,
-64.5269330036509, -64.5508330036509, -64.9324130036509, -66.4002496703509,
-66.4605896703509, -66.6230763369509, -66.6636963369509, -66.6425830036509,
-66.5310230036509, -66.4582830036509, -66.2992030036509, -65.8810363369509,
-65.3338363369509, -65.2480363369509, -65.3705963369509, -65.8357874342282,
-66.7324643369709, -66.8768896703509, -66.8215363369509, -66.8320584884004
), Lat = c(63.9018749538395, 64.1357216205395, 64.4444682872395,
64.4580016205395, 64.4744549538395, 64.4951416205395, 64.5202416205395,
64.5237216205395, 64.5388016205395, 64.5400516205395, 64.5090116205395,
64.5069516205395, 64.4609016205395, 64.2904882872395, 64.1898016205395,
63.9022816205395, 63.9948082872395, 64.0236682872395, 64.1115882872395,
64.2171216205395, 64.3599949538395, 64.3979682872395, 64.4634216205395,
64.4719816205395, 64.4459016205395, 64.4008282316608, …Run Code Online (Sandbox Code Playgroud) 这是我尝试过的事情的列表。
---
title: "test_gif"
output: html_document
---
``` {r, animation.hook='gifski', dev='png', interval=0.2}
library(gganimate)
ggplot(airquality, aes(Day, Temp, group = Month)) +
geom_line() +
transition_reveal(Month)
```
Run Code Online (Sandbox Code Playgroud)
错误:从第 8-12 行退出 (test_gif.Rmd) hook_animation(options)(x, options) 中的错误:要使用 hook_gifski(),代码块必须生成“png”图像而不是“gif”。调用: ... hook_plot -> hook_plot_md_base -> hook_plot_html -> 执行停止
尽管我使用了此处提到的 dev = 'png' https://yihui.name/en/2018/08/gifski-knitr/,但我无法使其工作。
然后我尝试使用 FFmpeg 渲染器
---
title: "test_gif"
output: html_document
---
```{r, animation.hook='ffmpeg', interval=0.2}
library(gganimate)
ggplot(airquality, aes(Day, Temp, group = Month)) +
geom_line() +
transition_reveal(Month) -> p
animate(p)
```
Run Code Online (Sandbox Code Playgroud)
错误:执行: ffmpeg -y -r 5 -i test_gif_files/figure-html/unnamed-chunk-1-%d.gif …
我有以下生成静态图表的 R 脚本
library(ggplot2)
library(dplyr)
library(tidyr)
library(stringr)
library(gtable)
library(cowplot)
library(ggrepel)
library(tidyquant)
library(gganimate)
library(gifski)
################################## BASIC SETUP TASKS ###############################
plotDate="May19"
basepath = "C:/Users/your output path here/"
#Countries with testing data reported
#going to leave out Spain because their data is so sporadic
testCountries <- c("ARG", "AUS", "AUT","BEL","BGD","BHR","BGR","BOL","CAN","CHE",
"CHL","COL","CRI","CUB","CZE","DEU","DNK","ECU","EST",
"ETH","FIN","FRA","GBR","GHA","GRC","HKG","HRV","HUN","IDN",
"IND","IRL","IRN","ISL","ISR","ITA","JPN","KAZ","KEN","KOR",
"LTU","LUX","LVA","MAR","MEX","MMR","MYS","NGA","NLD","NOR",
"NPL","NZL","PAK","PAN","PER","PHL","POL","PRT","PRY","ROU",
"RUS","RWA","SEN","SGP","SLV","SRB","SVK","SVN","SWE","THA",
"TUN","TUR","TWN","UGA","URY","USA","VNM","ZAF")
# Get population data
url <- "https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/CSV_FILES/WPP2019_TotalPopulationBySex.csv"
pops <- read.csv(url, stringsAsFactors = FALSE, header = TRUE)
pops <- pops %>% filter(Time==2020) %>% select(Location, PopTotal) %>% distinct()
pops$PopTotal …Run Code Online (Sandbox Code Playgroud) 我想m^2 m^-2使用gganimate. 例如,当我只使用它时,下面给了我我想要的输出ggplot2:
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
labs(title = "Hp" ~ m^2 ~ m^-2)
Run Code Online (Sandbox Code Playgroud)
但是,当我执行以下操作时,gganimate我没有得到 Hp 的变化值,但是{closest_state}
library(gganimate)
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
transition_states(hp) +
labs(title = expression('Hp = {closest_state}' ~ m^2 ~ m^-2))
Run Code Online (Sandbox Code Playgroud)
我怎么能解决这个问题?
是否可以在动画循环结束后停止?
动画应该执行到去年 2021 年,然后停止显示所有条形!
以下是带有 的条形动画示例gganimate:
library(tidyverse)
library(gganimate)
year <- (2000:2021)
something <- mtcars$mpg[1:22]
df <- tibble(year,something)
p <- ggplot(df, aes(year, something))+
geom_col() +
geom_text(aes(label=something), vjust=-0.3, size=4)+
transition_states(year, transition_length = 5, state_length = 10) + shadow_mark() +
enter_grow() +
exit_fade(alpha = 1)+
labs(title = 'Year: {closest_state}',
subtitle = "Blabla",
caption = "bla")
animate(p, fps=8)
Run Code Online (Sandbox Code Playgroud)