I've created an animation below using gganimate but if you look at the GIF, the last point doesn't get plotted. Could some one help me figure out why this is happening. Also, if you notice, the 4th point on the orange line is plotted with a delay. How can I fix this? Any ideas?
library(ggplot2)
library(gganimate)
library(dplyr)
library(readxl)
### Load the blueberry price data
blueberries_data <- read_excel("Volume_Price.xlsx")
### Select rows 1 to 42 in the "Value" column and divide by 1,000,000
blueberries_data[1:42, "Value"] <- blueberries_data[1:42, "Value"] / 1000000
blueberries_data$Year <- as.factor(blueberries_data$Year)
### Define line and point colors
line_colors <- c("Price" = "#33d1ae", "Volume" = "#ff9326")
point_colors <- c("Price" = "#33d1ae", "Volume" = "#ff9326")
### Create an animated plot for weekly blueberry prices
p <- ggplot(blueberries_data, aes(x = Week)) +
geom_line(data = filter(blueberries_data, Year == "Price"), aes(y = Value, color = "Price"), show.legend = TRUE) +
geom_line(data = filter(blueberries_data, Year == "Volume"), aes(y = Value * 5, color = "Volume"), show.legend = TRUE) +
geom_point(aes(y = ifelse(Year == "Volume", Value * 5, Value), group = seq_along(Week), color = Year), size = 2, alpha = 1) +
labs(y = 'Weekly Blueberry Prices', color = "") +
scale_color_manual(values = line_colors) +
scale_y_continuous(
name = "Price",
sec.axis = sec_axis(~. / 5, name = "Volume"),
breaks = seq(0, 60, 10) # Adjust the breaks to extend to 60 on the primary y-axis
) +
lims(x = c(0, 45), y = c(0, 60)) + # Set x and y-axis limits
theme(legend.position = "bottom", # Set legend position
panel.background = element_rect(fill = "white"), # Set panel background color to white
panel.grid.major.y = element_line(color = "grey89"), # Set major horizontal grid lines to grey
panel.grid.minor.y = element_blank()) # Remove minor horizontal grid lines
anim_plot <- p + transition_reveal(Week)
animate(anim_plot, fps = 10, duration = 10, end_pause = 40) # Adjust the frames per second (fps) as needed
anim_save("Blueberry_weekly_prices.gif", last_animation(), width = 2, height = 2)
This issue seems to be caused by
transition_reveal()
. Your issue can be demonstrated usinggapminder
dataset.To fix the issue, (full credit to @Z.Lin and the functions are defined here) you can define
transition_reveal2()
as shown below.