<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/how-the-ghana-floods-animation-was-created/"></div></div><div><a data-url="https://www.r-bloggers.com/how-the-ghana-floods-animation-was-created/" data-counturl="https://www.r-bloggers.com/how-the-ghana-floods-animation-was-created/" data-count="horizontal" data-text="How the Ghana Floods animation was created" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://datascience-africa.org/2016/11/ghana-floods-animation-created/"> R – Data Science Africa</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Ghana has always been getting flooded, but it seems that only floods in Accra are getting attention. I wrote about it <a target="_blank" rel="nofollow" href="https://medium.com/@DaveQuartey/like-accra-other-places-are-being-flooded-where-e1c67b90d5b7">here</a>, and the key visualization was an animated map showing the floods in Ghana, and built in R. In this post, I document how I did it, hopefully you can do one also!<br>
<img alt="file1ce037965c4d" class="wp-image-406" src="https://i2.wp.com/datascience-africa.org/wp-content/uploads/2016/09/file1ce037965c4d-1.gif?resize=456%2C313" data-recalc-dims="1"></p>
<p>Before we start, there are a number of things we will need:</p>
<li>Flooding data</li>
<li>Packages</li>
<p>Lets load the R packages we will need:<br>
<code>library(ggmap)<br>
library(ggplot2)<br>
library(gganimate)</code><br>
<code>ggmap</code> is what we will use to pull the Ghana Map from google. <code>ggplot2</code> to plot the points and <code>gganimate</code> to animate.</p>
<p><strong>Data</strong></p>
<p>Green Ghanaian did an amazing job of putting together a Ghana Flood League Table so I placed it in a .csv file here.<br>
Download it and lets get our hands dirty.<br>
We will import the .csv file into R using the <code>read.csv()</code> function.<br>
<code>g &lt;- read.csv(&quot;ghanafloods_data.csv&quot;)</code></p>
<p>What this does is, it pulls the data from the location stated into R.</p>
<p><code>head(g)</code><br>
<code><br>
city lat lon Legend date<br>
## 1 Komenda 5.120700 -1.4628042 Flood Incident 13/06/2016<br>
## 2 Eguafo 5.163801 -1.4132753 Flood Incident 13/06/2016<br>
## 3 Oda Nkwata 5.936244 -0.9796825 Flood Incident 15/06/2016<br>
## 4 Tema 5.708924 0.0186555 Flood Incident 09/06/2016<br>
## 5 Accra 5.603717 -0.1869644 Flood Incident 09/06/2016<br>
## 6 Breman Asikuma 5.592330 -0.9958237 Flood Incident 04/06/2016<br>
</code><br>
We then convert the date in the data to an R recognizable format:<br>
<code>g$date &lt;- as.Date(g$date, format = &quot;%d/%m/%Y&quot;)<br>
head(g)</code><br>
<code><br>
## city lat lon Legend date<br>
## 1 Komenda 5.120700 -1.4628042 Flood Incident 2016-06-13<br>
## 2 Eguafo 5.163801 -1.4132753 Flood Incident 2016-06-13<br>
## 3 Oda Nkwata 5.936244 -0.9796825 Flood Incident 2016-06-15<br>
## 4 Tema 5.708924 0.0186555 Flood Incident 2016-06-09<br>
## 5 Accra 5.603717 -0.1869644 Flood Incident 2016-06-09<br>
## 6 Breman Asikuma 5.592330 -0.9958237 Flood Incident 2016-06-04<br>
</code></p>
<p>Order or arrange the flood data through its date.<br>
<code>g &lt;- g[order(g$date),]<br>
head(g)</code><br>
<code><br>
## city lat lon Legend date<br>
## 19 Yendi 9.445044 -0.0093266 Flood Incident 2016-03-07<br>
## 18 Kumasi 6.666600 -1.6162709 Flood Incident 2016-03-14<br>
## 17 Nkrankese 6.150761 -1.4434690 Flood Incident 2016-03-16<br>
## 16 Atomfourso 7.720724 -2.5173175 Flood Incident 2016-03-23<br>
## 15 Zabzugu 9.278363 0.3748354 Flood Incident 2016-03-26<br>
## 14 Ho 6.610149 0.4785495 Flood Incident 2016-03-29<br>
</code></p>
<p><strong>Ghana Map</strong><br>
We’ll use the ggmap library to pull the Ghana map into R. First, lets load the ggmap package into our workspace together with the ggplot2 package because ggmap depends on it.<br>
<code>library(ggplot2)<br>
library(ggmap)</code></p>
<p>We now import the Ghana map using the <code>get_map()</code> function and wrap it in <code>ggmap()</code> to plot.<br>
<code>p &lt;- ggmap(get_map(&quot;Ghana&quot;, zoom = 7))<br>
print(p)</code><br>
<img sizes="(max-width: 672px) 100vw, 672px" alt="download" class="wp-image-426" src="https://i0.wp.com/datascience-africa.org/wp-content/uploads/2016/10/download.png?resize=456%2C326" data-recalc-dims="1"><br>
In <code>get_map()</code>, we set the argument <code>location</code> as “Ghana” and zoom in or out of that specified location with zoom argument. <code>zoom = 7</code> works fine. You can play around with it using numbers ranging from 3(zoomout) to 21(zoomin).</p>
<p><strong>Plot!</strong><br>
Now that we have our Ghana Map in R, it’s time to plot!</p>
<p>We want to plot the points on the map. This can be done using <code>geom_point()</code>. The x axis becomes the longitude and the y axis becomes the latitube.<br>
<code>p + geom_point(data = g, aes(x = lon, y = lat))<br>
</code><img sizes="(max-width: 672px) 100vw, 672px" alt="download-1" class="wp-image-436" src="https://i2.wp.com/datascience-africa.org/wp-content/uploads/2016/10/download-1.png?resize=456%2C326" data-recalc-dims="1"><br>
We could color the points using the col argument in the aesthetics function, with the <code>scale_color_manual()</code> used to override the default point color or to change the colour.</p>
<p><code>p + geom_point(data = g, aes(x = lon, y = lat, col = Legend)) +<br>
scale_color_manual(values =&quot;red1&quot;)</code><br>
<img sizes="(max-width: 672px) 100vw, 672px" alt="download-2" class="wp-image-428" src="https://i2.wp.com/datascience-africa.org/wp-content/uploads/2016/10/download-2.png?resize=456%2C326" data-recalc-dims="1"><br>
We could further increase the point size.<br>
<code><br>
p + geom_point(data = g, aes(x = lon, y = lat, col = Legend), size = 7) + scale_color_manual(values =&quot;red1&quot;)<br>
</code><br>
<img sizes="(max-width: 672px) 100vw, 672px" alt="download-3" class="wp-image-429" src="https://i1.wp.com/datascience-africa.org/wp-content/uploads/2016/10/download-3.png?resize=456%2C326" data-recalc-dims="1"><br>
We then make the points a little more transparent by assigning it with an alpha value. This is done to make it visible when points overlap. <code>alpha = 0.3</code> works fine here.<br>
<code><br>
p + geom_point(data = g, aes(x = lon, y = lat, col = Legend), size = 7, alpha = 0.3) + scale_color_manual(values =&quot;red1&quot;)<br>
</code><br>
<img sizes="(max-width: 672px) 100vw, 672px" alt="download-4" class="wp-image-430" src="https://i2.wp.com/datascience-africa.org/wp-content/uploads/2016/10/download-4.png?resize=456%2C326" data-recalc-dims="1"></p>
<p>A title is needed.<br>
<code>p + geom_point(data = g, aes(x = lon, y = lat, col = Legend), size = 7, alpha = 0.3) + scale_color_manual(values =&quot;red1&quot;) + labs(title = &quot;Ghana Floods from March, 2016 - July, 2016 \n&quot;)<br>
</code><br>
<img sizes="(max-width: 672px) 100vw, 672px" alt="download-5" class="wp-image-431" src="https://i2.wp.com/datascience-africa.org/wp-content/uploads/2016/10/download-5.png?resize=456%2C326" data-recalc-dims="1"></p>
<p><strong>Lets do some Animation, shall we?</strong></p>
<p>Before we start we’ll need to have either GraphicsMagick or ImageMagick installed for animations.</p>
<p>Regardless of the one you choose to install, make sure it is in your PATH so that you don’t run into problems when using it in R.</p>
<p>Also we would need to insert the cumulative and frame arguments in our plot aethetics. Our arguments are <code>cumulative = TRUE</code> which will allows R to biuld up on the points it plots. <code>frame = date</code> tells R to change the animation frame by the date column, thats why it’s important to change the date to an R-readable format here.<br>
<code><br>
p &lt;- p + geom_point(data = g, aes(x = lon, y = lat, frame = date, col = Legend,cumulative = TRUE), size = 7, alpha = 0.3) + scale_color_manual(values =&quot;red1&quot;) + labs(title = &quot;Ghana Floods from March, 2016 - July, 2016 \n&quot;)</code><br>
<code>#convert = &quot;im convert&quot; if using ImageMagick or convert = &quot;gm convert&quot; if using GraphicMagick<br>
gg_animate(p, outfile = &quot;outfile.gif&quot;, convert = &quot;gm convert&quot;, ani.width = 700, title_frame = TRUE)<br>
</code><br>
<img alt="file1ce037965c4d" class="wp-image-406" src="https://i2.wp.com/datascience-africa.org/wp-content/uploads/2016/09/file1ce037965c4d-1.gif?resize=456%2C313" data-recalc-dims="1"></p>
<p>And viola!</p>
<p>All the code used for this post can be found on my github <a target="_blank" rel="nofollow" href="https://github.com/DavidQuartey/Ghana_Flood/blob/master/ghanafloods.R">here</a>.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://datascience-africa.org/2016/11/ghana-floods-animation-created/">How the Ghana Floods animation was created</a> appeared first on <a target="_blank" rel="nofollow" href="http://datascience-africa.org/">Data Science Africa</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/how-the-ghana-floods-animation-was-created/"></div></div><div><a data-url="https://www.r-bloggers.com/how-the-ghana-floods-animation-was-created/" data-counturl="https://www.r-bloggers.com/how-the-ghana-floods-animation-was-created/" data-count="vertical" data-text="How the Ghana Floods animation was created" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://datascience-africa.org/2016/11/ghana-floods-animation-created/"> R – Data Science Africa</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/BfSr1S_8Cqc"></a></div>