
source("gmdata.R")

library(lattice)
library(gridSVG)

# Single-panel lattice plot
###########################
print(
xyplot(X1950.y ~ X1950.x, data=all,
       xlim=c(1, 9),
       ylim=c(10, 90),
       xlab="Fertility\n(# children per woman)",
       ylab="Life Expectancy (at birth)",
       subscripts=TRUE,
       # Draw circles until we can animate points
       panel=function(x, y, subscripts, ...) {
           grid.circle(x, y, default.units="native",
                       r=radius[subscripts]*unit(.25, "inch"),
                       name=trellis.grobname("points", type="panel"),
                       gp=gpar(col=all$colours[subscripts],
                         fill=adjustcolor(all$colours[subscripts], alpha=.5),
                         lwd=2))
       })
)

xCols <- grep("[.]x$", names(all))
yCols <- grep("[.]y$", names(all))

# Try to animate 
grid.animate(trellis.grobname("points", type="panel", row=1, col=1),
             duration=20,
             x=t(as.matrix(all[xCols])),
             y=t(as.matrix(all[yCols])))

gridToSVG("gapminderOnePanel.svg")

# Multi-panel lattice plot
###########################
x11(width=10, height=7)
print(
xyplot(X1950.y ~ X1950.x | continent, data=all,
       xlim=c(1, 9),
       ylim=c(10, 90),
       xlab="Fertility\n(# children per woman)",
       ylab="Life Expectancy (at birth)",
       subscripts=TRUE,
       # Draw circles until we can animate points
       panel=function(x, y, subscripts, ...) {
           # Having all data as grey circles makes it too slow/jerky

           # grid.circle(all$X1950.x, all$X1950.y, default.units="native",
           #             r=radius*unit(.25, "inch"),
           #             name=trellis.grobname("bg", type="panel"),
           #             gp=gpar(col=NA, fill=rgb(.7, .7, .7, .5)))
           
           # Text labels
           for (i in 1:length(subscripts)) {
               cg <- circleGrob(x[i], y[i], default.units="native",
                                r=radius[subscripts[i]]*unit(.25, "inch"),
                                name=paste("point", subscripts[i], sep=""),
                                gp=gpar(col=all$colours[subscripts[i]],
                                  fill=adjustcolor(all$colours[subscripts[i]],
                                    alpha=.5),
                                  lwd=2))
               cgg <- garnishGrob(cg,
                                  onmouseover=paste("showLabel(evt, '",
                                    all$fertility[subscripts[i]], "')",
                                    sep=""),
                                  onmouseout=paste("hideLabel()", sep=""))
               acgg <- animateGrob(cgg,
                                   duration=20,
                                   x=all[subscripts[i], xCols],
                                   y=all[subscripts[i], yCols])
               grid.draw(acgg)
               tg <- textGrob(all$fertility[subscripts[i]],
                              x[i], y[i], default.units="native",
                              name=paste("country", subscripts[i], sep=""))
               tgg <- garnishGrob(tg,
                                  visibility="hidden")
               grid.draw(tgg)
           }
       })
)


# Having all data as grey circles makes it too slow/jerky
# for (i in 1:2) {
#     for (j in 1:3) {
#         grid.animate(trellis.grobname("bg", type="panel", row=i, col=j),
#                      duration=20,
#                      x=t(as.matrix(all[xCols])),
#                      y=t(as.matrix(all[yCols])))
#     }
# }

# Add text label for year
years <- 1950:2009
visibility <- matrix("hidden", nrow=length(years), ncol=length(years))
diag(visibility) <- "visible"
yearText <- animateGrob(garnishGrob(textGrob(years, .9, .05,
                                             name="year",
                                             gp=gpar(cex=2, col="grey")),
                                    visibility="hidden"),
                        duration=20,
                        visibility=visibility)
grid.draw(yearText)

# Add javascript to show country names
grid.script(file="labels.js")

gridToSVG("gapminderMultiPanel.svg")
dev.off()
