I would like to add symbols to the leaves of a dendrogram reflecting variable for sites on a dendrogram similar to this:
require(graphics)
hc <- hclust(dist(USArrests[1:5,]), "ave")
plot(hc)
plot(hc, hang = -1)
USArrests[1:5,]
Murder Assault UrbanPop Rape
Alabama 13.2 236 58 21.2
Alaska 10.0 263 48 44.5
Arizona 8.1 294 80 31.0
Arkansas 8.8 190 50 19.5
California 9.0 276 91 40.6
Thanks for any suggestion on how to go about this
SOLUTION Following Backlin's helpful suggestions I use the following solution
require(graphics)
hc <- hclust(dist(USArrests[1:5,]), "ave")
plot(hc, hang = -1, xlab="", sub="")
col.circle=c("yellow", "red")[cut(USArrests$Murder[hc$order], c(8,10,15))]
symbols(1:5, rep(-25, 5), circles=rep(1, 5), add=TRUE, inches=.2,bg=col.circle, xpd=TRUE)
col.square=c("blue", "green")[cut(USArrests$Assault[hc$order], c(100,200,300))]
symbols(1:5, rep(-35, 5), squares=rep(1, 5), add=TRUE, inches=.4,bg=col.square, xpd=TRUE)
legend(3.7,85,legend=c("Murder 8-10","Murder 10-15","Assualt 100-200","Assualt 200-300"),fill=c("yellow","red","blue","green"))
The symbols
function could be used to accomplish this in a somewhat roundabout, but still effective way. It does not support triangles, but has a few other shapes to choose from. In the demonstration below, note the parameter xpd=TRUE
that allows you to draw outside the plot area, i.e. in the margin.
plot(hc, hang = -1, xlab="", sub="")
symbols(1:5, rep(-25, 5), circles=rep(1, 5), add=TRUE, inches=.2,
bg=rep(c("grey", "red"), c(3,2)), xpd=TRUE)
symbols(1:5, rep(-35, 5), squares=rep(1, 5), add=TRUE, inches=.4,
bg=rep(c("grey", "red"), c(1,4)), xpd=TRUE)
For readability of the example, the y coordinates of the symbols are set to absolute values. If you want to make them relative to the coordinates of the plot use something like the following, where par("usr")
is a vector with (x-left, x-right, y-bottom, y-top) of the plot area.
y = par("usr")[3] - .04 * diff(par("usr")[3:4])
The legend can also be plotted with symbol
and text
. The idea is the same and you can probably figure it out yourself, even though it is quite fiddly.