Interactive graph demonstration using a modified version of B. Rowlingson's imagemap package:
As you drag your mouse over the map, data from the 1977 US census data (a dataset that comes with R) will appear. When you click on a state, a graph will be created that shows rankings for that state.
source(RpadBaseFile('imagemap.R'))
source(RpadBaseFile('html_extra_utils.R'))
require(maps)
data(state)
im = imagemap("Test", width = 600, height = 300)
# draw the map
par(mar=c(0,0,0,0))
m = map("state", fill=T, col=0)
# make the image map boundaries
marker = which(is.na(m$x))
startidx = c(1, marker + 1)
endidx = c(marker - 1, length(m$x))
for (i in seq(along = m$names))
addRegion(im) = imPoly(cbind(m$x[startidx[i]:endidx[i]],
m$y[startidx[i]:endidx[i]]),
onmouseover =
paste("javascript:document.getElementById('results').innerHTML =",
" data[", i-1, "]", sep=""),
onclick =
paste("javascript:document.getElementById('selectedregion').value=",
i-1, ";rpad.calculatePage()", sep="")
)
imClose(im)
# fix up the names and look at the rankings
rownames(state.x77) = tolower(rownames(state.x77))
regionnames = gsub(":.*", "", m$names)
state.rank = 51-apply(state.x77,FUN=rank,MARGIN=2)
# create a javascript data array and stuff it with HTML results for
# use in the browser
# it doesn't need to go back to the server for this
cat('data = Array(')
for (i in seq(along = m$names))
if (regionnames[i] %in% rownames(state.x77)) {
cat('"', HTMLh3(regionnames[i]),
paste(capture.output(Html(state.x77[regionnames[i],,drop=FALSE])), collapse=" "),
'",\n', sep = "")
} else {
cat('" ",')
}
cat('" ");\n')
#cat('alert("l="+data[50]);\n')
# do the HTML
HTMLon()
HTMLtag("span", contenteditable="false")
# create a hidden INPUT to hold the selected region (passed to R)
HTMLinput(name="selectedregion", id="selectedregion", value=1, type="hidden", rpadType="Rvariable")
# create a DIV to display the map data
HTMLtag("div", id="results")
cat(" ")
HTMLetag("div")
# display the image with the imagemap
createIM(im)
HTMLetag("span")
newgraph()
dotchart(state.rank[regionnames[selectedregion+1],],
xlim = c(0,50),
pch = 19,
xlab = "State ranking",
lcolor = "black")
HTMLon()
HTMLh3(regionnames[selectedregion+1])
HTMLtag('div', style="height:330px") # fixed-height wrapper prevents jumpiness
showgraph()
HTMLetag('div')