Reputation: 170
I want to make a plot animate. I need that the points appear and disappear, but only make that the movements of points.
set.seed(1)
library(tidyverse)
library(gganimate)
df <- tibble(
x = rnorm(100)
, y = rnorm(100)
, size = rep(c(2, 3, 4, 5), 25)
, cl = sample(c("a", "b"), 100, T)
, time = rep(1:10, 10) #|> lubridate::year()
)
p2 <-
df |>
ggplot() +
aes(x, y, size = size, color = cl) +
geom_point() +
scale_size(range = c(5, 12)) +
transition_reveal(time) +
#enter_fade()
#transition_manual(time) +
shadow_mark(past = F, future = T)
animate(p2, renderer = gifski_renderer("sa.gif"))
Upvotes: 2
Views: 390
Reputation: 18714
If you don't want to animate by rows or by variable, just all at once, you can use any of the enter
states and transition_layers
. It won't show the final transition, but it will only animate the entrance. I added enter_fade()
so you would see the entrance.
p2 <-
df |>
ggplot() +
aes(x, y, size = size, color = cl) +
geom_point() +
scale_size(range = c(5, 12)) +
theme_bw() +
transition_layers(layer_length = 2,
transition_length = 2) +
enter_fade()
In your comment, you asked if you could first should the group a
, then group b
. I assume this is what you're looking for.
p3 <- ggplot() +
geom_point(data = filter(df, cl == "a"),
aes(x, y, color = cl, size = size)) +
geom_point(data = filter(df, cl == "b"),
aes(x, y, color = cl, size = size)) +
scale_size(range = c(5, 12)) +
theme_bw() +
transition_layers(layer_length = 2,
transition_length = 2) +
enter_fade()
p3
Upvotes: 2