VOOZH about

URL: https://www.analyticsvidhya.com/blog/2017/06/a-study-on-global-seismic-activity-between-1965-and-2016/

⇱ How To Create GIF Images | Gganimate Package To Create GIF


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

How to create animated GIF images for data visualization using gganimate (in R)?

Guest Blog Last Updated : 26 Jul, 2020
5 min read

Introduction

Data visualization is probably the most important and typically the least talked about area of data science.

I say that because how you create data stories and visualization has a huge impact on how your customers look at your work. Ultimately, data science is not only about how complicated and sophisticated your models are. It is about solving problems using data based insights. And in order to implement these solutions, your stakeholders need to understand what you are proposing.

One of the challenges in creating effective visualizations is to create images which speak for themselves. This article will tell one of the ways to do so using animated GIF images (Graphics Interchangeable format). This would be particularly helpful when you want to show time / flow based stories. Using animation in images, you can plot comparable data over time for specific set of parameters. In other words, it is easy to understand and see the growth of certain parameter over time.

Let me show this with an example

Example – GDP vs. Life expectancy over time

Let us say you want to show how GDP and life expectancy have changed for various continents / countries over time. What do you think is the best way to represent this relationship?

You can think of multiple options like:

  • Creating a 3D plot with GDP, life expectancy and time on 3 plots and draw lines for each continent / country. The problem is that human eye is really bad as interpreting 3D visualizations in 2D. Especially so, if there is too much data. So, this option would not work.
  • Creating 2 plots side by side – one showing GDP over time and other life expectancy over time. While this is a 2 dimensional plot, we have left a lot for user to interpret. The person need to pick a country and see its movement on each plot and then correlate them. Again, I would ask this from my stakeholders.

Now, let us look at this using an animated plot using .gif file:

👁 Image

The recent development of gganimate package had made this possible and easier. By the end of this article, you will be able to make your own .gif file and create your own customised frame to compare different parameters on global or local scale.

Pre-requisites

Please install the following packages:

  • ggmap
  • gganimate
  • dplyr
  • animation

In addition to the above libraries in R, you will also need Image Magick Software in your system. You may download and install the same from Image Magick

Get the Data

This article is an attempt to make .gif file on earthquake data from 1965-2016. It is better to plot year wise global seismic activity rather than a static look of all the values on the map. The data set for earthquake is available on Kaggle.
The data set contains data for global seismic activity from 1965 to 2016. Please visit the above link and scroll down to get the .csv file.

Earthquake magnitude of 7 points on Richter Scale from 1965-2016

The dataset had been modified and only seismic value of 7 points on richter scale has been considered for the study.

Data Manipulation

From the .csv file we have only selected few parameters for the sake of simplicity.

  • Date
  • Time
  • Latitude
  • Longitude
  • Type is the type of seismic activity
  • Depth is the distance of the epicenter from the seal level.
  • Magnitude is the reading on the richter scale
  • ID is the event ID of the seismic activity

We are all set to start coding in R. I have used RStudio environment. You are free to use any environment you prefer.

R Codes

## Read the datatset and load the necessary packages
library(plyr)
library(dplyr)
library(ggmap)
library(ggplot2)
library(gganimate)
EQ=read.csv("eq.csv",stringsAsFactors = FALSE)
names(EQ)
## Only Select the data with magnitude greater than or equal to 7.
EQ<-EQ%>%filter(Magnitude>=7)

Split the Date into year, month and date

This is done in order to get the frame which is important for the plot. In other words, The core of the approach is to treat frame
(as in, the time point within an animation) as another dimension, just like x, y, size, color, or so on. Thus, a variable in your data can be mapped to frame just as others are mapped to x or y.

## Convert the dates into character in order to split the coloumn into "dd" "mm" "yy"" columns
EQ$Date<-as.character(EQ$Date)

## Split the date and create a list for the same

list<-strsplit(EQ$Date,"-")

## Convert the list into dataframe
library(plyr)
EQ_Date1<-ldply(list)
colnames(EQ_Date1)<-c("Day","Month","Year")

## Column bind with the main dataframe
EQ<-cbind(EQ,EQ_Date1)
names(EQ)
## Change the Date to numeric
EQ$Year=as.numeric(EQ$Year)

## Get the world map for plot and load the necessary package
library(ggmap)
world<-map_data("world")

## Remove Antarctica region from the world map

world <- world[world$region != "Antarctica",]
map<-ggplot()+geom_map(data=world,map=world,aes(x=long,y=lat,map_id=region),color='#333300',fill='#663300')

#Plot points on world Map

p <- map + geom_point(data = EQ, aes(x = Longitude, y = Latitude, 
 frame = Year, 
 cumulative = TRUE,size=EQ$Magnitude), alpha = 0.3, 
 size = 2.5,color="#336600")+
 geom_jitter(width = 0.1) +labs(title = "Earthquake above 7 point on richter scale")+theme_void()

# Plot .gif file using gganimate function

gganimate(p)
👁 Image

Earthquake

Speed up projection in .gif using animation package

As we can see that plot has too many years from 1965 to 2016. Thus, in order to speed up the visualization, we can use the animation package to fast forward using ani.option()

library(animation)
ani.options(interval=0.15)
gganimate(p)
👁 Image

Earthquake – 1.5x speed

Conclusion

This article was an introductory tutorial to the world of animated map. Readers can try this and apply the same in other projects. Some of the example are,

  • The same technology can be used to compare the heat map for the weather data across nation
  • Flood or other natural disaster in a particular location over a period of time.
  • Can be used to see the growth of metro in city using delaunay triangle. Please see the interesting article posted by Page Piccinini in r-Bloggers, Metro Systems over Time or you can directly access her page from her official site Page Piccinin.

Hope you found the article useful. If you have any questions, please feel free to ask in comments below.

👁 Image
Aritra Chatterjee is a professional in the field of Data Science and Operation Management having experience of more than 5 years. He aspires to develop skill in the field of Automation, Data Science and Machine Learning.

This post was received as part of our blogging competition – The Mightiest Pen. Check out other competitions here.

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Kartik Patnaik

Awsome! Aritra, Great Work! Keep going..

123 1
Aritra Chatterjee

Thank you, Kartik.

123 456
Credendina Draco

gganimate package is not available for R version 3.4.0. How can I get it to work?

123 2
Arunoday Sinha

To isntall gganimate, follow below steps: library(devtools) install.packages("Rcpp") devtools::install_github("dgrtwo/gganimate") library(gganimate)

123 456
Aritra Chatterjee

Please install cowplot package and also update ggplot2 package before installing gganimate.

123 456
Vineet Kapoor

great blog , learning blog

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
👁 Av Logo White

Continue your learning for FREE

Forgot your password?
👁 Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

👁 Popup Banner
👁 AI Popup Banner