Pages

Sunday, June 1, 2014

Malawi Animated Rainfall Map

Couple of months ago, Charles Langton Vanya of Malawi contacted me on how to make animated rainfall map  in R. I was very busy that I was not able to respond. Anyway, to Mr. Vanya, here is the code you can play on to produce a similar map as below.























14 comments:

  1. Cool sir, but I suggest, for good syntax highlighting simply change the gistfile1.txt to gistfile1.R :)

    ReplyDelete
  2. Salvacion, Thank you! this is indeed what I was looking for. Its good that now I can be able to follow what is happening. What about Asaad's comment, where is this change supposed to be done? Once again, this is very good work. Thank you!
    Charles Vanya

    ReplyDelete
  3. Dear Salvacion,
    After I tried the code, I am face with few challenges, one of it is my lack of understanding of the error found? Installation of Magic is not properly done in my machine because the path is different to what the code expect. Here is how I tried it

    > library(raster)
    Loading required package: sp
    Warning message:
    package ‘sp’ was built under R version 3.0.3
    > library(maptools)
    Checking rgeos availability: TRUE
    Warning message:
    package ‘maptools’ was built under R version 3.0.3
    > library(rgdal)
    rgdal: version: 0.8-16, (SVN revision 498)
    Geospatial Data Abstraction Library extensions to R successfully loaded
    Loaded GDAL runtime: GDAL 1.10.1, released 2013/08/26
    Path to GDAL shared files: C:/Program Files/R/R-3.0.2/library/rgdal/gdal
    GDAL does not use iconv for recoding strings.
    Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
    Path to PROJ.4 shared files: C:/Program Files/R/R-3.0.2/library/rgdal/proj
    > library(animation)
    Warning message:
    package ‘animation’ was built under R version 3.0.3
    > library(RColorBrewer)
    >
    > malb<-getData("GADM", country="MWI", level=1)
    > malp<-getData("worldclim", var="tmin", res=0.5, lon=35, lat=-14)
    > malpe<-extract(wmap,malb,fun=mean, na.rm=T)
    Error in extract(wmap, malb, fun = mean, na.rm = T) :
    error in evaluating the argument 'x' in selecting a method for function 'extract': Error: object 'wmap' not found
    > malpe<-data.frame(malpe)
    Error in data.frame(malpe) : object 'malpe' not found
    > malb@data<-malpe
    Error: object 'malpe' not found
    > dat<-colnames(malb@data)
    > malb@data$id<-row.names(malb@data)
    > malbf<-fortify(malb, region="id")
    Error: could not find function "fortify"
    > malb.df<-merge(malbf,malb@data, by="id")
    Error in merge(malbf, malb@data, by = "id") :
    error in evaluating the argument 'x' in selecting a method for function 'merge': Error: object 'malbf' not found
    > colnames(malb.df)[8:19]<-c("January","February","March","April","May","June","July","August","September","October","November","December")
    Error in colnames(malb.df)[8:19] <- c("January", "February", "March", :
    object 'malb.df' not found
    >
    > col<-brewer.pal(9,"Spectral")
    > for (i in 8:19){
    + var<-colnames(malb.df)[i]
    + p<-ggplot(malb.df, aes(long,lat, group=group))+geom_polygon(aes_string(fill=var))+coord_equal()
    + p<-p+geom_polygon(data=malb, aes(long,lat.group=group),fill=NA, colour="black")
    + p<-p+scale_fill_gradientn("Rainfall",limits=c(0,330),guide="legend", breaks=seq(0,330,30), colours=col)
    + p<-p+theme_bw()+ggtitle(var)
    + print(p)
    + ggsave(file=paste("pr",i,".png", sep=""))
    + }
    Error in is.data.frame(x) : object 'malb.df' not found
    >
    > files = sprintf('pr%d.png', 8:19)
    > im.convert(files, output = 'Rainfall.gif')
    I cannot find ImageMagick with convert = "convert"
    but I can find it from the Registry Hive: C:\Program Files\ImageMagick-6.8.8-Q8
    Executing:
    "C:\Program Files\ImageMagick-6.8.8-Q8\convert.exe" -loop 0 -delay 100
    pr8.png pr9.png pr10.png pr11.png pr12.png pr13.png pr14.png
    pr15.png pr16.png pr17.png pr18.png pr19.png
    "C:\Users\CHARLES\AppData\Local\Temp\Rtmporl5BV/Rainfall.gif"
    convert.exe: unable to open image `pr8.png': No such file or directory @ error/blob.c/OpenBlob/2645.
    convert.exe: unable to open file `pr8.png' @ error/png.c/ReadPNGImage/4021.

    >

    ReplyDelete
  4. Hi Charles,

    Please check the R version you are using and download image magic and check over the internet on how to properly install it on your machine.

    ReplyDelete
  5. Salvacion, I got the error below when I tried to run your code. Could you please help me? Thanks...

    Error in extract(wmap, malb, fun = mean, na.rm = T) :
    error in evaluating the argument 'x' in selecting a method for function 'extract': Error: object 'wmap' not found

    ReplyDelete
  6. Hi Unknown and Charles,

    I have updated the code. You can retry to run it again.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  7. Yes you have to load ggplot2.

    ReplyDelete
    Replies
    1. Salvacion, Thank you for the help. I am now able to plot maps using the scripts. However, I am facing challenges in changing the order of how color can be plotted. For example:

      col<-brewer.pal(9,"Spectral")

      gives me red as lower values while blue is higher values. How can change this when plotting temperatures so that red should indicate higher values and viceversa?

      Delete
  8. AnonymousJune 05, 2014

    Error: isTRUE(gpclibPermitStatus()) is not TRUE

    maptools is loaded.

    What do I do?

    ReplyDelete
    Replies
    1. Hi Rafael,

      Try to install or load rgeos package.

      HTH

      Arnold

      Delete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Thank you very much for your nice post and it helped me to reproduce my own plot and video to the country of my interest.

    Thanks

    ReplyDelete