Animated Graph With R

titlelinkabstractyear
Misophonia: current perspectiveshttps://www.tandfonline.com/doi/abs/10.2147/NDT.S81438Misophonia is characterized by a negative reaction to …2015
2015
2014
R
# Needed packages installations 
## pacman helps to install a package and load the library in one line of code
install.packages("pacman")
pacman::p_load(ggplot2, gganimate, ggpubr, tidyverse, png, gifski, readr)

# Set working directory or choose from the shortcut CTRL + MAJ + H
setwd("your_directory_path_here")

# Loading raw data 
data_raw <- read_csv("Google_scholar_results.csv")

# Change the column year to numeric entry if not the case
data_raw$year <- as.numeric(data_raw$year)

# Remove rows where the "Year" column has NA
data_raw <- data_raw[complete.cases(data_raw$year), ]

# Filter rows and keep only specific years (here 2010 and above)
## The pipe operator %>% below allows to manipulate data structures without calling the dataframe repetitively
## It enables you to chain multiple operations together in a readable and concise manner
data_filter <- data_raw %>%   
filter(year >= 2010)

# Group by "Year" and calculate the count for each year
result <- data_filter %>%
group_by(year) %>%
summarize(count = n())

# Store the result in a new data frame
plot_data <- data.frame(Year = result$year, Count = result$count)

# Load background image for the plot
image_load <- "worldmap.png"

# Read the PNG image
img <- readPNG(image_load)

# Static plot
plot <- ggplot(plot_data, aes(x = Year, y = Count)) +
background_image(img) + 
geom_line(color= "white", size = 2, alpha = 1) + # Alpha: 1 is opaque
geom_point(aes(group = seq_along(Year)), color = "white", size = 5) +
# Displays point/each year
theme(
plot.background = element_rect(fill = "black"), # Set background to black
axis.text = element_text(color = "white", size = 20, face="bold"),
axis.title.x = element_text(color = "white", size = 25, face="bold"), 
    axis.title.y = element_text(color = "white", size = 25, face="bold"), 
plot.title = element_text(color = "white", size = 28, face="bold"), 
panel.grid.major = element_blank(), # Remove major and minor grid lines
panel.grid.minor = element_blank() 
) +
labs(
title = "Trends in Scientific Publications on Misophonia over Time", 
x = "Year", 
    y = "Count"
) + 
scale_x_continuous(breaks = unique(data_filter$year)) 
# Display unique years on the x-axis (in case of duplicates)

# Set the size of the plot, only for the static plot
options(repr.plot.width = 20, repr.plot.height = 10)

# Display the static plot and make adjustments (theme, text size, etc.)
print(plot)

R
# Animated plot from the static plot previously created
## Use transition_reveal to animate the plot based on the 'Year' variable or any variable of interest
anim_static <- plot + transition_reveal(Year)

# Settings of the animation including plot size, duration and duration of the end_pause
animate_plot <- 
animate(
anim_static,
duration = 7, # Duration of the entire animation/seconds
width = 1200, # Width of the animated plot
height = 800, # Height of the animated plot
end_pause = 50, # Duration of the pause at the end
start_pause = 10, # Duration of the pause at the start 
renderer = gifski_renderer() # Render GIF animations 
)

# Save the animated plot as a GIF file
gganimate::anim_save("misophonia_animation_R.gif", animate_plot)