I am trying to add a line to my animation, but I couldn't make it work using the concept of frame. This is a reproducible example:
df <- read.table(header = TRUE, text = 'key value bins maxIntensity
A 4 0 1
A 1 1 1
A 0 2 1
B 3 0 2
B 2 1 2
B 5 2 2
D 2 0 1
D 3 1 1
D 0 2 1')
the animation can be created using gganimate package:
library('animation')
library('gganimate')
par(bg = "white")
g <- ggplot(df, aes(xmin = df$bins, xmax = df$bins + 1, ymin = 0, ymax = df$value, frame = df$key))
g <- g + geom_rect(fill=alpha("Orange", alpha = 1))
g <- g + labs(title = "Test Histogram")
g <- g + scale_y_continuous(labels = scales::comma)
gganimate(g, ani.width=400, ani.height=400, interval = .4, "test.gif")
Which works just fine.
Now I would like to add a line, at a different location for each frame. The location is specified in df$maxIntensity
.
So, I think I should add this:
g <- g + geom_vline(xintercept = df$maxIntensity, lty=3, color = "black")
but that simply adds all the lines at once, at each frame. Any idea how add one line to each frame?