简体   繁体   中英

Repositioning and increasing the weight of borders around dendrogram produced with R 'plot' function

I am trying to cut a dendrogram into three classes using the rect.hclust function, but when I export the graph, it cuts off the borders at the bottom of the graph. In addition, I would like to increase the weight of the borders, but I am not sure how to do this, as the lwd argument doesn't seem to exist for this function. What can I do to fix these parameters?

Data:

cluster <- data.frame(plot=c(1,
                             2,
                             3,
                             4,
                             5,
                             6,
                             7,
                             8,
                             9,
                             10,
                             11,
                             12,
                             13,
                             14,
                             15,
                             16,
                             17,
                             18,
                             19,
                             20),
                      meas1 = c(443,
                                836,
                                903,
                                684,
                                94,
                                125,
                                733,
                                846,
                                625,
                                234,
                                437,
                                775,
                                269,
                                774,
                                17,
                                502,
                                80,
                                51,
                                523,
                                229),
                      meas2 = c(735,
                                574,
                                793,
                                261,
                                961,
                                136,
                                404,
                                138,
                                45,
                                935,
                                698,
                                675,
                                594,
                                497,
                                152,
                                153,
                                30,
                                667,
                                547,
                                745),
                      meas3 = c(23,
                                526,
                                36,
                                93,
                                708,
                                970,
                                399,
                                111,
                                456,
                                439,
                                569,
                                503,
                                337,
                                213,
                                399,
                                850,
                                614,
                                491,
                                28,
                                452))

Code for hierarchical clustering and dendrogram generation:

#Generate distance matrix
dist_mat <- dist(cluster, method = 'euclidean')

#Hierarchical clustering 
hclust_avg <- hclust(dist_mat, method = 'average')

#Cut into 3 classes
cut_avg <- cutree(hclust_avg, k = 3)

#Plot dendrogram
plot(hclust_avg, xlab = "", ylab = "Euclidean Distance", sub = "", 
     main = "Cluster Dendrogram", lwd = 2)

#Generate borders around each group
rect.hclust(hclust_avg, k=3, border = 2:6)

Exporting the graph looks as follows: 在此处输入图像描述

I need the red-green-blue borders to be thicker (higher border weights), and I also need them to not be cut off at the bottom of the graph.

I bypassed the problem of the missing lower edge of the rectangle by setting the figure margins to zero before drawing the rectangle.

The line width of the rectangles can be set by setting par(lwd), eg par(lwd=4) , as in the example below:

cluster <- data.frame(plot=1:20,
                      meas1 = c(443,
                                836,
                                903,
                                684,
                                94,
                                125,
                                733,
                                846,
                                625,
                                234,
                                437,
                                775,
                                269,
                                774,
                                17,
                                502,
                                80,
                                51,
                                523,
                                229),
                      meas2 = c(735,
                                574,
                                793,
                                261,
                                961,
                                136,
                                404,
                                138,
                                45,
                                935,
                                698,
                                675,
                                594,
                                497,
                                152,
                                153,
                                30,
                                667,
                                547,
                                745),
                      meas3 = c(23,
                                526,
                                36,
                                93,
                                708,
                                970,
                                399,
                                111,
                                456,
                                439,
                                569,
                                503,
                                337,
                                213,
                                399,
                                850,
                                614,
                                491,
                                28,
                                452))


#Generate distance matrix
dist_mat <- dist(cluster, method = 'euclidean')

#Hierarchical clustering 
hclust_avg <- hclust(dist_mat, method = 'average')

#Cut into 3 classes
cut_avg <- cutree(hclust_avg, k = 3)

pars <- par()
#Plot dendrogram
plot(hclust_avg, xlab = "", ylab = "Euclidean Distance", sub = "", 
     main = "Cluster Dendrogram", lwd = 2)
par(lwd=4, mar=c(0,0,0,0))
#Generate borders around each group
rect.hclust(hclust_avg, k=3, border = 2:6)


# reset par
par(lwd=pars$lwd, mar=pars$mar)

Created on 2020-06-30 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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