Objective

The objective of the assignment was to tell a story on terrorism through data visualisation.

Reference

Code

library(gganimate)
library(hrbrthemes)
library(ggplot2)
library(readr)
library(dplyr)

Data <- read_csv("fatalities-from-terrorism.csv")

data<- Data %>% group_by(Year) %>% arrange(-`Terrorism fatalities (GTD, 2018) (deaths)`) %>% mutate(rank=row_number()) %>% filter(rank<=10)


p<-data %>%
    ggplot(aes(x = -rank,y = `Terrorism fatalities (GTD, 2018) (deaths)`, group = Entity ,frame = Year)) +
        geom_tile(aes(y = `Terrorism fatalities (GTD, 2018) (deaths)` / 2, height = `Terrorism fatalities (GTD, 2018) (deaths)`, fill = Region), width = 0.9) +
        geom_text(aes(label = Entity), hjust = "right", colour = "black", fontface = "bold", nudge_y =0) +
        geom_text(aes(label = scales::comma(`Terrorism fatalities (GTD, 2018) (deaths)`)), hjust = "left", nudge_y = 10000, colour = "grey30") +
        coord_flip(clip="off")+ 
        scale_x_reverse() +
        scale_fill_manual(name = 'Region', values = c("#66c2a5", "#fc8d62", "#8da0cb", "#e78ac3","#f1a340","#e41a1c","#ffffb3","#b3de69","#d9d9d9"))+scale_x_discrete("") +
        scale_y_continuous("",labels=scales::comma) +
        hrbrthemes::theme_ipsum(plot_title_size = 32, subtitle_size = 24, caption_size = 20, base_size = 20) +
        theme(
        legend.position="right",
        panel.background=element_blank(),
        panel.border=element_blank(),
        plot.background=element_blank(),
        plot.margin = margin(2, 2, 2, 4, "cm")) +
        # gganimate code to transition by year:
        transition_time(Year) +
        ease_aes('cubic-in-out') +
        labs(title='Number of Fatalites from Terrorism',
             subtitle='Deaths in {round(frame_time,0)}',
             caption='Source: Our World in Data', caption_size = 20)
 
        

animate(p, nframes = 750, fps = 15, end_pause = 50, width = 1200, height= 900)

#anim_save("test")