Pages

Tuesday, August 7, 2012

Provincial Monthly Rainfall of the Philippines from WORLDCLIM

Preparing for a future conference on climate change, I downloaded and extracted average monthly rainfall in the Philippines from worldclim.org. Using maptools, raster, and animation package in R, I produced an animation of average monthly rainfall of the country.


7 comments:

  1. Hi,arsalvacion. It's a very nice graph.
    I am working in New Zealand. I am wanting to produce something very similar to your stuff.
    I am wondering if you can share your code with me.

    Thanks in advance.
    Yang.
    My email address is gylamb@gmail.com

    ReplyDelete
  2. Hi Yang,

    I just save each file as png (e.g. Rainfall1.png to Rainfall12.png) the use the code (below) sent to me by Yihui Xie to produce the GIF file.


    library(animation)
    files = sprintf('Rainfall%d.png', 1:12)
    im.convert(files, output = 'Rainfall.gif')

    Best regards,

    ARSalvacion

    ReplyDelete
    Replies
    1. Hi ARSalvacion,
      I liked your animated example. However, when I tried to do the same with my own data, I am unable to display the data by polygons just like what you have done.
      Can you please show me the code you have used to display such kind of beautiful map?
      Vanya Charles

      Delete
  3. Hi, arsalvacion! I was led onto your blog through the R-bloggers. Nice graph!
    I'm doing a paper on institutions and poverty. I want to include geophysical attributes, including average rainfall and average elevation, in my model to control for potential development. Do you have any idea where to get these data by municipality, or how I can generate them?

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi Arnold, nice graph. thank you.
    I tried to do something similar with data from Puerto Rico, but I have a couple errors when I try to run the commands, since for example here rains more than 5000mm.

    malb<-getData("GADM", country="PR", level=1)
    malp<-getData("worldclim", var="prec", res=0.5, lon=-65, lat=18)
    malpe<-extract(malp,malb,fun=mean, na.rm=T)
    malpe<-data.frame(malpe)
    malb@data<-malpe
    dat<-colnames(malb@data)
    malb@data$id<-row.names(malb@data)
    malbf<-fortify(malb, region="id")
    malb.df<-merge(malbf,malb@data, by="id")
    colnames(malb.df)[8:19]<-c("January","February","March","April","May","June","July","August","September","October","November","December")

    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,6000),guide="legend", breaks=seq(0,6000,500), colours=col)
    p<-p+theme_bw()+ggtitle(var)
    print(p)
    ggsave(file=paste("pr",i,".png", sep=""))
    }

    files = sprintf('pr%d.png', 8:19)
    im.convert(files, output = 'Rainfall.gif')

    thanks for you help

    Pablo

    ReplyDelete