[R] horizontal labels for a dendrogram

Juhász Péter peter.juhasz83 at gmail.com
Mon Sep 26 18:25:40 CEST 2011


Dear R-help list,

I'd like to create visualize the clustering of a dataset with a
dendrogram. I'm using the following script:

data = read.table("data.csv", header=T, sep=";")
require(cluster)
res = as.dendrogram(agnes(data))
chlab <- function(n) {
 if(is.leaf(n)) {
  att <- attributes(n)
  labx <- data$category1[att$label]
  lab_color <- ifelse(labx == "A", "black", ifelse(labx == "B", "red",
"green"))
  attr(n, "nodePar") <- c(att$nodePar, lab.col = lab_color, pch = NA)
  attr(n, "label") <- labx
 }
 n
}
# lab.col = lab_color,
res2 = dendrapply(res, chlab)
print(res2)
pdf("dendrogram.pdf",width=5,height=2.5,pointsize=8,paper='special') 
par(mai=c(0.5,0.5,0.2,0.2)) 
plot(res2)
dev.off()

This works nicely with the exception of one detail: the labels are
perpendicular to the normal reading direction. I'm aware of the
leaflab="textlike" option which does put the labels horizontally,
however, it also draws rectangles around the labels which I don't want.

How can I create a dendrogram with horizontal labels, without those
rectangles?

Some example data for the script:

code;name;sex;date1;exp_type;exp1;category1;comment1;sar1;distance1;order1;n_trials;run1_name_cond1_rt;run1_name_cond2_rt;run1_name_cond3_rt;run1_name_cond1_er;run1_name_cond2_er;run1_name_cond3_er;run1_col_cond1_rt;run1_col_cond2_rt;run1_col_cond4_rt;run1_col_cond1_er;run1_col_cond2_er;run1_col_cond4_er;run2_name_cond1_rt;run2_name_cond2_rt;run2_name_cond3_rt;run2_name_cond1_er;run2_name_cond2_er;run2_name_cond3_er;run2_col_cond1_rt;run2_col_cond2_rt;run2_col_cond4_rt;run2_col_cond1_er;run2_col_cond2_er;run2_col_cond4_er
00;xxx;F;Tue Sep  6 12:53:19
2011;umts;0;C;;1.80;5.0;0;20;1009.35;1394.65;1046.95;0;0.05;0;1222.25;1136.75;1012.65;0;0;0;884.7;1004.8;1003.45;0;0;0;813.3;1045.75;946;0;0;0
01;xxx;N;Tue Sep  6 14:51:18
2011;umts;1;B;;1.80;5.0;1;20;1042.15;1073.6;1087.7;0;0;0;1501.45;1932.6;1210.8;0;0.2;0;805.9;776.35;800.35;0;0;0;937.65;1094.1;917.6;0;0.05;0
02;xxx;F;Wed Sep  7 10:49:16
2011;lte;1;A;;1.80;5.0;0;20;1084.05;912;769.45;0.05;0.1;0;785.2;1137;925.6;0;0.1;0;955.1;1013.45;955;0;0.05;0.05;917.6;1383.4;876.45;0;0.1;0
03;xxx;F;Wed Sep  7 15:00:56
2011;lte;0;C;;1.80;5.0;1;20;1226.65;1329.6;1218.95;0;0;0;1371.9;2126.25;1356.05;0;0.1;0;1065.25;1379.15;1091.75;0.05;0.05;0;1370.7;2654.75;1314.55;0;0.15;0
04;xxx;F;Thu Sep  8 09:20:03

Thanks in advance for your answer:

Péter Juhász



More information about the R-help mailing list