paths <- PSimport("tiger.ps.out") # drop the first element (just a bounding rect?) paths <- paths[-1] # normalise the locations minoutlinex <- function(x) { min(x@x, na.rm=TRUE) } minx <- min(sapply(outlines, minoutlinex)) minoutliney <- function(x) { min(x@y, na.rm=TRUE) } miny <- min(sapply(outlines, minoutliney)) maxoutlinex <- function(x) { max(x@x, na.rm=TRUE) } maxx <- max(sapply(outlines, maxoutlinex)) maxoutliney <- function(x) { max(x@y, na.rm=TRUE) } maxy <- max(sapply(outlines, maxoutliney)) cx <- mean(c(minx, maxx)) cy <- mean(c(miny, maxy)) range <- max(maxx - minx, maxy - miny) normalise <- function(x, c) { unit(0.5 + (x - c)/range, "npc") } setGeneric("norm", function(x) { standardGeneric("norm") }) setMethod("norm", signature(x="PSstroke"), function(x) { linesGrob(normalise(x@x, cx), normalise(x@y, cy), gp=gpar(lwd=x@lwd, col=x@rgb)) }) setMethod("norm", signature(x="PSfill"), function(x) { polygonGrob(normalise(x@x, cx), normalise(x@y, cy), gp=gpar(col=NA, fill=x@rgb)) }) # Produce a plot of tiger populations with picture as background # Source: http://www.globaltiger.org/population.htm year <- c(1993, 1996, 1998, 2001) minpop <- c(20, 50, 50, 115) maxpop <- c(50, 240, 240, 150) # Draw a plot pdf("tiger.pdf", version="1.4") grid.rect(gp=gpar(col=NA, fill="light grey")) pushViewport(plotViewport(), viewport(xscale=c(1991, 2003), yscale=c(0, 250))) # tiger backdrop pushViewport(viewport(width=0.95, height=0.95), viewport(width=unit(1, "snpc"), height=unit(1, "snpc"))) grid.draw(do.call("gList", lapply(outlines, norm))) popViewport(2) # translucent cover grid.rect(gp=gpar(fill=rgb(1, 1, 1, 0.8))) grid.xaxis(at=year) grid.yaxis() grid.rect(x=unit(year, "native"), y=0, width=unit(1, "native"), height=unit(maxpop, "native"), just="bottom", gp=gpar(fill=rgb(0, 0, 1, 0.2))) grid.text("Estimated Population (max.) of Bengal Tigers\n(in Bhutan)", y=unit(1, "npc") + unit(2, "lines")) popViewport(2) grid.text("Source: http://www.globaltiger.org/population.htm", x=unit(1, "npc") - unit(2, "mm"), y=unit(1, "lines"), just="right", gp=gpar(fontfamily="mono", cex=0.5)) dev.off()