SANDHEEP KUMAR V B
SANDHEEP KUMAR V B

Reputation: 11

Dendrogram in R by complete linkage not spaced properly

I am a newbie with R. To speak in a very understandable way, What I want to achieve is a dendrogram like this What I want the dendrogram to look like

and how I get it is like this, How I am getting it

This is the code, that I ran,

tb <- read.csv("COM_PDT.csv", row.names = 1)
> d = as.dist(tb)
> hc.c <- hclust(d)
> plot(hc.c, hang = -1)

And here is the data set,

,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48
1,,0,0,0,0,0,0,0,3,1,4,0,4,0,3,0,0,4,0,0,0,4,0,0,1,1,4,3,0,3,3,0,1,0,4,4,0,0,0,0,1,0,3,0,0,0,4,1
2,0,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,2,3,1,0,0,0,1,0,0,0,0
3,0,0,,2,0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1
4,0,0,2,,1,1,1,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1,0,1,1,0,1,0,1,1,0,1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,1
5,0,0,0,1,,3,0,0,0,1,0,1,0,0,1,0,2,0,2,1,1,0,3,2,3,0,0,1,2,1,1,1,2,2,0,0,2,1,1,1,1,2,0,0,2,2,0,0
6,0,0,0,1,3,,0,1,0,2,0,2,0,1,1,1,3,0,3,1,1,0,4,2,3,0,0,1,3,1,1,0,3,3,0,0,2,0,0,0,2,1,0,0,3,3,0,1
7,0,0,1,1,0,0,,3,0,0,0,2,0,3,0,2,1,0,0,1,3,0,0,2,0,1,0,0,1,0,0,0,0,1,0,0,2,0,0,0,1,1,0,0,1,1,0,2
8,0,0,1,1,0,1,3,,0,1,0,3,0,4,0,3,2,0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1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,3,2,,0,2,0,0,0,0,0,0
41,1,0,0,0,1,2,1,2,1,2,1,3,1,2,2,2,3,1,2,2,2,1,2,2,1,0,1,1,3,1,1,0,3,3,1,1,2,0,0,0,,0,1,0,3,3,1,3
42,0,0,1,2,2,1,1,1,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,2,0,,0,0,0,0,0,1
43,3,0,0,0,0,0,0,0,3,1,3,0,3,0,2,0,0,3,0,0,0,3,0,0,0,1,3,2,0,2,2,0,1,0,3,3,0,0,0,0,1,0,,0,0,0,3,1
44,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0
45,0,0,0,0,2,3,1,2,0,1,0,3,0,2,1,2,4,0,3,2,2,0,3,3,2,0,0,0,4,0,0,0,2,4,0,0,3,0,0,0,3,0,0,0,,4,0,2
46,0,0,0,0,2,3,1,2,0,1,0,3,0,2,1,2,4,0,3,2,2,0,3,3,2,0,0,0,4,0,0,0,2,4,0,0,3,0,0,0,3,0,0,0,4,,0,2
47,4,0,0,0,0,0,0,0,3,1,4,0,4,0,3,0,0,4,0,0,0,4,0,0,1,1,4,3,0,3,3,0,1,0,3,4,0,0,0,0,1,0,3,0,0,0,,1
48,1,0,1,1,0,1,2,3,1,2,1,2,1,3,1,3,2,1,1,1,1,1,1,1,0,1,1,1,2,1,1,0,2,2,1,1,1,0,0,0,3,1,1,0,2,2,1,

Please help me to get a clean dendrogram that is neatly spaced and the end nodes properly at the floor of the graph!

Upvotes: 1

Views: 66

Answers (1)

Leo
Leo

Reputation: 198

I think you need to write dist(tb), not as.dist(tb). That will help the branching appearance of how it is being plotted. Change hang = to adjust the labeling, though initially I haven't been able to produce exactly how the labels are in your desired plot.

d = dist(tb)
hc.c <- hclust(d)
plot(hc.c, hang = -1)

Upvotes: 1

Related Questions