How the Ghana Floods animation was created

Ghana has always been getting flooded, but it seems that only floods in Accra are getting attention. I wrote about it here, 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!
file1ce037965c4d

Before we start, there are a number of things we will need:

  • Flooding data
  • Packages
  • Lets load the R packages we will need:
    library(ggmap)
    library(ggplot2)
    library(gganimate)

    ggmap is what we will use to pull the Ghana Map from google. ggplot2 to plot the points and gganimate to animate.

    Data

    Green Ghanaian did an amazing job of putting together a Ghana Flood League Table so I placed it in a .csv file here.
    Download it and lets get our hands dirty.
    We will import the .csv file into R using the read.csv() function.
    g <- read.csv("ghanafloods_data.csv")

    What this does is, it pulls the data from the location stated into R.

    head(g)

    city lat lon Legend date
    ## 1 Komenda 5.120700 -1.4628042 Flood Incident 13/06/2016
    ## 2 Eguafo 5.163801 -1.4132753 Flood Incident 13/06/2016
    ## 3 Oda Nkwata 5.936244 -0.9796825 Flood Incident 15/06/2016
    ## 4 Tema 5.708924 0.0186555 Flood Incident 09/06/2016
    ## 5 Accra 5.603717 -0.1869644 Flood Incident 09/06/2016
    ## 6 Breman Asikuma 5.592330 -0.9958237 Flood Incident 04/06/2016

    We then convert the date in the data to an R recognizable format:
    g$date <- as.Date(g$date, format = "%d/%m/%Y")
    head(g)


    ## city lat lon Legend date
    ## 1 Komenda 5.120700 -1.4628042 Flood Incident 2016-06-13
    ## 2 Eguafo 5.163801 -1.4132753 Flood Incident 2016-06-13
    ## 3 Oda Nkwata 5.936244 -0.9796825 Flood Incident 2016-06-15
    ## 4 Tema 5.708924 0.0186555 Flood Incident 2016-06-09
    ## 5 Accra 5.603717 -0.1869644 Flood Incident 2016-06-09
    ## 6 Breman Asikuma 5.592330 -0.9958237 Flood Incident 2016-06-04

    Order or arrange the flood data through its date.
    g <- g[order(g$date),]
    head(g)


    ## city lat lon Legend date
    ## 19 Yendi 9.445044 -0.0093266 Flood Incident 2016-03-07
    ## 18 Kumasi 6.666600 -1.6162709 Flood Incident 2016-03-14
    ## 17 Nkrankese 6.150761 -1.4434690 Flood Incident 2016-03-16
    ## 16 Atomfourso 7.720724 -2.5173175 Flood Incident 2016-03-23
    ## 15 Zabzugu 9.278363 0.3748354 Flood Incident 2016-03-26
    ## 14 Ho 6.610149 0.4785495 Flood Incident 2016-03-29

    Ghana Map
    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.
    library(ggplot2)
    library(ggmap)

    We now import the Ghana map using the get_map() function and wrap it in ggmap() to plot.
    p <- ggmap(get_map("Ghana", zoom = 7))
    print(p)

    download
    In get_map(), we set the argument location as “Ghana” and zoom in or out of that specified location with zoom argument. zoom = 7 works fine. You can play around with it using numbers ranging from 3(zoomout) to 21(zoomin).

    Plot!
    Now that we have our Ghana Map in R, it’s time to plot!

    We want to plot the points on the map. This can be done using geom_point(). The x axis becomes the longitude and the y axis becomes the latitube.
    p + geom_point(data = g, aes(x = lon, y = lat))
    download-1
    We could color the points using the col argument in the aesthetics function, with the scale_color_manual() used to override the default point color or to change the colour.

    p + geom_point(data = g, aes(x = lon, y = lat, col = Legend)) +
    scale_color_manual(values ="red1")

    download-2
    We could further increase the point size.

    p + geom_point(data = g, aes(x = lon, y = lat, col = Legend), size = 7) + scale_color_manual(values ="red1")

    download-3
    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. alpha = 0.3 works fine here.

    p + geom_point(data = g, aes(x = lon, y = lat, col = Legend), size = 7, alpha = 0.3) + scale_color_manual(values ="red1")

    download-4

    A title is needed.
    p + geom_point(data = g, aes(x = lon, y = lat, col = Legend), size = 7, alpha = 0.3) + scale_color_manual(values ="red1") + labs(title = "Ghana Floods from March, 2016 - July, 2016 \n")

    download-5

    Lets do some Animation, shall we?

    Before we start we’ll need to have either GraphicsMagick or ImageMagick installed for animations.

    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.

    Also we would need to insert the cumulative and frame arguments in our plot aethetics. Our arguments are cumulative = TRUE which will allows R to biuld up on the points it plots. frame = date 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.

    p <- 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 ="red1") + labs(title = "Ghana Floods from March, 2016 - July, 2016 \n")

    #convert = "im convert" if using ImageMagick or convert = "gm convert" if using GraphicMagick
    gg_animate(p, outfile = "outfile.gif", convert = "gm convert", ani.width = 700, title_frame = TRUE)

    file1ce037965c4d

    And viola!

    All the code used for this post can be found on my github here.

    Share on:Share on Facebook3Share on Google+0Tweet about this on TwitterShare on LinkedIn0Pin on Pinterest0Share on Tumblr0

    One thought on “How the Ghana Floods animation was created

    Leave a Reply

    Your email address will not be published. Required fields are marked *