2

我想为树状图上的站点添加符号到树状图反映变量的叶子,类似于以下内容:

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    

在此处输入图像描述

感谢您提供有关如何解决此问题的任何建议

解决方案按照 Backlin 的有用建议,我使用以下解决方案

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"))

在此处输入图像描述

4

1 回答 1

4

symbols功能可用于以某种迂回但仍然有效的方式完成此操作。它不支持三角形,但有一些其他形状可供选择。在下面的演示中,请注意xpd=TRUE允许您在绘图区域之外绘制的参数,即在边距中。

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)

在此处输入图像描述

为了示例的可读性,符号的 y 坐标设置为绝对值。如果要使它们相对于绘图的坐标,请使用以下内容,其中par("usr")是绘图区域的 (x-left, x-right, y-bottom, y-top) 的向量。

y = par("usr")[3] - .04 * diff(par("usr")[3:4])

图例也可以用symbol和绘制text。这个想法是一样的,你可能会自己弄清楚,即使它很繁琐。

于 2012-08-29T10:54:52.670 回答