https://www.youtube.com/c/TechAnswers88

youtube video link with explanations for these examples https://youtu.be/Srf8JC2dcqs

Create an animated chart using the ggplot and gganimate packages

Packages used

library(rio)
library(ggplot2)
library(dplyr)
library(gganimate)
library(transformr)
## Warning: package 'transformr' was built under R version 4.1.2

Get the data from the following URL

https://data.worldbank.org/indicator/NY.GDP.MKTP.KD.ZG?end=2019&locations=IN&name_desc=false&start=2019&view=map

Download the csv file on your computer and give the appropriate path as shown below https://api.worldbank.org/v2/en/indicator/NY.GDP.MKTP.KD.ZG?downloadformat=csv

The ruling party information was sourced from the following URL https://en.wikipedia.org/wiki/List_of_current_Indian_ruling_and_opposition_parties

# url is the location of your computer eg. c:\\tmp\\yourdata.csv"

url <- "E:\\tmp\\API_NY.GDP.PCAP.KD.ZG_DS2_en_csv_v2_3358272\\API_NY.GDP.PCAP.KD.ZG_DS2_en_csv_v2_3358272.csv"


df <- read.csv(url, skip = 4,header = TRUE)

d <- df%>%
     dplyr::filter(Country.Name == 'India')

dparty <- data.frame(Year = seq(1960,2020, by = 1)
                    ,RulingParty = c('INC','INC', 'INC', 'INC', 'INC', 'INC', 'INC', 'INC', 'INC', 'INC', 'INC', 'INC', 'INC', 'INC', 'INC', 'INC', 'INC'
                                     , 'JP','JP','JP','INC(I)'
                                     ,'INC(I)','INC(I)','INC(I)','INC','INC','INC','INC','INC','INC','INC','INC' , 'INC','INC','INC','INC'
                                     ,'BJP','BJP','BJP','BJP','BJP','BJP','BJP','BJP'
                                     ,'INC','INC','INC','INC','INC','INC','INC','INC','INC', 'INC'
                                     , 'BJP', 'BJP', 'BJP', 'BJP', 'BJP', 'BJP', 'BJP'
                                     )  )

dlong <- d%>%
         dplyr::select(Country.Name, starts_with("X"))%>%
         tidyr::pivot_longer(cols = starts_with("X"))%>%
         dplyr::mutate(Year = as.integer(substr(name,2,5) ))

ddata <- merge(dlong, dparty, a.x = TRUE)


pl <- ggplot(data = ddata, aes(x = (Year), y  = value, colour = RulingParty, fill = RulingParty, group = 1 ))
pl <- pl + geom_line()
pl <- pl + geom_point(size = 5)
pl <- pl + geom_label(aes(label = paste0(RulingParty,'\n', round(value,1) ,"%")), vjust = -0.5, color = "black")
pl <- pl + labs(title = "GDP per capita growth (annual %) in India in recent years")
pl <- pl + labs(subtitle = "Data source:https://data.worldbank.org")
pl <- pl + geom_hline(yintercept = 0, color = "black", size = 1)
pl <- pl + geom_hline(yintercept = 5, color = "blue")
pl <- pl + geom_hline(yintercept = -5, color = "red")
pl <- pl + theme_classic()
pl <- pl + labs(x = "Year", y = "Yearly GDP per capita change %")
pl <- pl + theme(panel.grid.minor = element_line(colour = "grey", size = 0.5)) 
pl <- pl +  scale_y_continuous(minor_breaks = seq(-10,10, 1),breaks = seq(-10,10, 1))
pl <- pl + transition_reveal(as.integer(Year)) 
pl