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.
Hi,arsalvacion. It's a very nice graph.
ReplyDeleteI 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
Hi Yang,
ReplyDeleteI 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
Hi ARSalvacion,
DeleteI 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
so nice Graph
ReplyDeleteHi, arsalvacion! I was led onto your blog through the R-bloggers. Nice graph!
ReplyDeleteI'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?
This comment has been removed by the author.
ReplyDeleteHi Arnold, nice graph. thank you.
ReplyDeleteI 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