#
###################################################
# SCRIPT FOR TEMPLATE TO PLOT SEQUENCE OF BUBBLE CHARTS  
# Created by Gottfried Pestal (Solv Consulting Ltd.)
# Version 1 - August 23, 2012
###################################################

# This script illustrates how to create a pdf file with 10 pages and 4 bubble charts on each page.


# create an array and fill it with parameters for plotting (3 random normal variables for 4 different cases, mean and sd changing over time)

sample.data <- array(NA,dim=c(10,3,4), dimnames=list(1980:1989,c("Var_1","Var_2","Size"),c("Loc_1","Loc_2","Loc_3","Loc_4")))

sample.data[,"Var_1","Loc_1"] <- rnorm(10,mean=seq(-15,15,length.out=10), sd=2)
sample.data[,"Var_2","Loc_1"] <- rnorm(10,mean=seq(-15,15,length.out=10), sd=2)
sample.data[,"Size","Loc_1"] <- seq(3,7,length.out=10)


sample.data[,"Var_1","Loc_2"] <- rnorm(10,mean=seq(15,-15,length.out=10), sd=1)
sample.data[,"Var_2","Loc_2"] <- rnorm(10,mean=seq(-15,15,length.out=10), sd=1)
sample.data[,"Size","Loc_2"] <- seq(7,3,length.out=10)


sample.data[,"Var_1","Loc_3"] <- rnorm(10,mean=seq(-20,20,length.out=10), sd=3)
sample.data[,"Var_2","Loc_3"] <- rnorm(10,mean=seq(15,-15,length.out=10), sd=4)
sample.data[,"Size","Loc_3"] <- seq(4,6,length.out=10)

sample.data[,"Var_1","Loc_4"] <- rnorm(10,mean=seq(20,20,length.out=10), sd=6)
sample.data[,"Var_2","Loc_4"] <- rnorm(10,mean=seq(-15,-15,length.out=10), sd=6)
sample.data[,"Size","Loc_4"] <- seq(5,2,length.out=10)



#custom function to create bubble plot with path
bubbleplot <- function(x.vec,y.vec,radius.vec,current.pt,xrange,yrange,xlabel,ylabel,maintitle){
	plot(x.vec[current.pt],y.vec[current.pt],xlim=xrange, ylim=yrange,type="p",col="darkblue", xlab=xlabel,ylab=ylabel,bty="n",cex=1.2,cex.lab=1.6,cex.axis=1.6)
	symbols(x.vec[current.pt],y.vec[current.pt],radius.vec[current.pt],add=TRUE, inches=FALSE, pch=19,fg="darkgray",bg="lightgray",xpd=TRUE)
	points(x.vec[current.pt],y.vec[current.pt],pch=19,col="darkblue")
	lines(x.vec,y.vec,col="darkblue")
	title(main=maintitle,adj=0.2,cex.main=1.8)
} # end custom function bubbleplot



# open the pdf file as a plotting device
# note: onefile = TRUE creates a new page in the same pdf file whenever the current device is full
pdf("FigRs_Templates_BubbleChart.pdf",width=11, height=8.5,onefile=TRUE)


# specify margins 
par(omi=c(0.5,0.5,0.5,0.5) , mai=c(0.9,0.7,0.2,0.2))

# divide the plotting device 3 into panels
layout(matrix(c(1,2,3,4), byrow=TRUE, nrow=2,ncol=2), heights=c(1,1),widths=c(1,1))



# loop through the 10 years
for(year in 1:10){

	
# create plot in panel 1
bubbleplot(x.vec=sample.data[,"Var_1","Loc_1"],y.vec=sample.data[,"Var_2","Loc_1"],radius.vec=sample.data[,"Size","Loc_1"],
	current.pt=year,xrange=c(-30,30),yrange=c(-30,30),xlabel="Variable 1",ylabel="Variable 2",maintitle="Area 1")




# create plot in panel 2
bubbleplot(x.vec=sample.data[,"Var_1","Loc_2"],y.vec=sample.data[,"Var_2","Loc_2"],radius.vec=sample.data[,"Size","Loc_2"],
	current.pt=year,xrange=c(-30,30),yrange=c(-30,30),xlabel="Variable 1",ylabel="Variable 2",maintitle="Area 2")


# add horizontal line to second plot, expanding it across the first plot
abline(h=0,xpd=NA, col="grey")


# create plot in panel 3
bubbleplot(x.vec=sample.data[,"Var_1","Loc_3"],y.vec=sample.data[,"Var_2","Loc_3"],radius.vec=sample.data[,"Size","Loc_3"],
	current.pt=year,xrange=c(-30,30),yrange=c(-30,30),xlabel="Variable 1",ylabel="Variable 2",maintitle="Area 3")


# add vertical line to third plot, expanding it across the first plot
abline(v=0,xpd=NA, col="grey")



# create plot in panel 4
bubbleplot(x.vec=sample.data[,"Var_1","Loc_4"],y.vec=sample.data[,"Var_2","Loc_4"],radius.vec=sample.data[,"Size","Loc_4"],
	current.pt=year,xrange=c(-30,30),yrange=c(-30,30),xlabel="Variable 1",ylabel="Variable 2",maintitle="Area 4")


# add horizontal and vertical lines to fourth plot, expanding it across the second and third plot
abline(v=0,xpd=NA, col="grey")
abline(h=0,xpd=NA, col="grey")




# label the page
mtext(year+1979,side=3,line=0.5, outer=TRUE, cex=2.5)
mtext("www.solv.ca/FigRs",side=1,line=0.5, outer=TRUE, cex=1)



} # end looping across year


# turn off plotting device and save pdf file
dev.off()


#