简体   繁体   中英

How to have x-axis span move with gganimate animation?

Using R, I am trying to make a line graph which is revealed left to right based on x-axis using gganimate. I have managed to do this but what I also wanted to do was make it so that the scale_x_continuous(limits = c(i-5,i+5)), ie there is a window around the point that is being revealed and the window will move along while the next point is being revealed.

I have tried many ways to get this including implementing some sort of loop in scale_x_continuous with and without aes(). Nothing seems to work. I am quite new with ggplot2 and especially with gganimate but I couldn't find any help online. I have a feeling the answer is probably quite simple and I just missed it.

Sort of like this but with gganimate:

类似的例子,但不是 gganimate

The following is some reproducible code to show you roughly what I've done so far.

library(ggplot2)
library(gganimate)
library(gifski)
library(png)


Step  <- c(1:50,1:50)
Name  <- c(rep("A",50), rep("B",50))
Value <- c(runif(50,0,10), runif(50,10,20))
Final <- data.frame(Step, Name, Value)

a <- ggplot(Final, aes(x = Step, y = Value, group = Name, color = factor(Name))) + 
 geom_line(size=1) + 
 geom_point(size = 2) + 
 transition_reveal(Step) + 
 coord_cartesian(clip = 'off') + 
 theme_minimal() +
 theme(plot.margin = margin(5.5, 40, 5.5, 5.5)) +
 theme(legend.position = "none") 

options(gganimate.dev_args = list(width = 7, height = 6, units = 'in', res=100))
animate(a, nframes = 100)

Don't use a transition , use a view . Eg:

ggplot(Final, aes(x = Step, y = Value, color = factor(Name))) + 
    geom_line(size = 1) + 
    geom_point() +
    view_zoom_manual(
        0, 1, pause_first = FALSE, ease = 'linear', wrap = FALSE,
        xmin = 1:40, xmax = 11:50, ymin = min(Final$Value), ymax = max(Final$Value)
    ) +
    scale_x_continuous(breaks = seq(0, 50, 2))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM