简体   繁体   English

在R中的树状图叶子上添加符号

[英]Adding symbols to dendrogram leaves in R

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 解决方案根据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"))

在此处输入图片说明

The symbols function could be used to accomplish this in a somewhat roundabout, but still effective way. 可以使用symbols功能以某种回旋方式但仍然有效的方式来完成此任务。 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, ie in the margin. 在下面的演示中,请注意参数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)

在此处输入图片说明

For readability of the example, the y coordinates of the symbols are set to absolute values. 为了示例的可读性,将符号的y坐标设置为绝对值。 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. 如果要使它们相对于图的坐标,请使用如下所示的内容,其中par("usr")是具有图的(x-left,x-right,y-bottom,y-top)的向量区域。

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

The legend can also be plotted with symbol and text . 图例也可以用symboltext绘制。 The idea is the same and you can probably figure it out yourself, even though it is quite fiddly. 这个想法是一样的,即使很奇怪,您也可以自己弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM