Greetings, I have perused the r-help mailing list archives for an answer to this question, without avail. I would like to color the "leaves" of a dendrogram plot based on a cutoff in one of the variables involved in the initial clustering. My input data is in the form of: B K Alameda 0.2475770 0.7524230 Alpine 0.4546784 0.5453216 Amador 0.6278610 0.3721390 essentially rows labeled by county name, with two variables: percent voted for B and percent voted for K. While it is obvious that this is somewhat of a contrived example, I intend to use this as a learning device. Here is the code used to create and plot the dendrogram: hc <- hclust(dist(y), "ave") dend <- as.dendrogram(hc) plot(dend, main="CA 2004 Election Results by County") An example of the output can be found here: http://casoilresource.lawr.ucdavis.edu/drupal/node/206?size=_original I have experimented with the edgePar and nodePar parameters for the plot.dendrogram() method, but have not been able to make sense of the output. The basis for setting the colors of the leaves in the dendrogram is a simple majority calculation: reds <- y[y$B > 0.5, ] blues <- y[y$K > 0.5, ] Such that leaves in the tree will be colored based on the membership in either of the two above groups. Is there a resource documenting how this might be accomplished? Any thoughts or ideas would be greatly appreciated. Cheers, Dylan -- Dylan Beaudette Soils and Biogeochemistry Graduate Group University of California at Davis 530.754.7341
Dylan Beaudette
2006-Mar-10 21:36 UTC
[R] coloring leaves in a hclust or dendrogram plot [solved]
On Thursday 09 March 2006 06:12 pm, Dylan Beaudette wrote:> Greetings, > > I have perused the r-help mailing list archives for an answer to this > question, without avail. > > I would like to color the "leaves" of a dendrogram plot based on a cutoff > in one of the variables involved in the initial clustering. > > My input data is in the form of: > B K > Alameda 0.2475770 0.7524230 > Alpine 0.4546784 0.5453216 > Amador 0.6278610 0.3721390 > > essentially rows labeled by county name, with two variables: percent voted > for B and percent voted for K. While it is obvious that this is somewhat of > a contrived example, I intend to use this as a learning device. > > Here is the code used to create and plot the dendrogram: > hc <- hclust(dist(y), "ave") > dend <- as.dendrogram(hc) > plot(dend, main="CA 2004 Election Results by County") > > An example of the output can be found here: > http://casoilresource.lawr.ucdavis.edu/drupal/node/206?size=_original > > > I have experimented with the edgePar and nodePar parameters for the > plot.dendrogram() method, but have not been able to make sense of the > output. > > The basis for setting the colors of the leaves in the dendrogram is a > simple majority calculation: > > reds <- y[y$B > 0.5, ] > blues <- y[y$K > 0.5, ] > > Such that leaves in the tree will be colored based on the membership in > either of the two above groups. > > Is there a resource documenting how this might be accomplished? > > Any thoughts or ideas would be greatly appreciated. > > Cheers, > > DylanReplying to my own post... Discovered the dendapply() function: reds <<- as.factor(row.names(y[y$B > 0.5, ])) blues <<- as.factor(row.names(y[y$K > 0.5, ])) #define a function for coloring and sizing node elements: colLab <- function(n) { if(is.leaf(n)) { a <- attributes(n) if ( length(which(blues == a$label)) == 1 ) { attr(n, "nodePar") <- c(a$nodePar, list(lab.col = "blue", lab.cex=.7, col="blue", cex=pop[n], pch=16 )) } else { attr(n, "nodePar") <- c(a$nodePar, list(lab.col = "red", lab.cex=.7, col="red", cex=pop[n], pch=16)) } } n } #modfiy dendrogram nodes and re-plot dend_colored <- dendrapply(dend, colLab) ...which did the trick http://casoilresource.lawr.ucdavis.edu/drupal/node/210 -- Dylan Beaudette Soils and Biogeochemistry Graduate Group University of California at Davis 530.754.7341
Christos Hatzis
2006-Mar-10 21:49 UTC
[R] coloring leaves in a hclust or dendrogram plot [solved]
See also the colored dendrogram in the R Graph Gallery. It has a couple of additional cool features. http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=79 -Christos Hatzis -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Dylan Beaudette Sent: Friday, March 10, 2006 4:37 PM To: r-help at stat.math.ethz.ch Subject: Re: [R] coloring leaves in a hclust or dendrogram plot [solved] On Thursday 09 March 2006 06:12 pm, Dylan Beaudette wrote:> Greetings, > > I have perused the r-help mailing list archives for an answer to this > question, without avail. > > I would like to color the "leaves" of a dendrogram plot based on a > cutoff in one of the variables involved in the initial clustering. > > My input data is in the form of: > B K > Alameda 0.2475770 0.7524230 > Alpine 0.4546784 0.5453216 > Amador 0.6278610 0.3721390 > > essentially rows labeled by county name, with two variables: percent > voted for B and percent voted for K. While it is obvious that this is > somewhat of a contrived example, I intend to use this as a learningdevice.> > Here is the code used to create and plot the dendrogram: > hc <- hclust(dist(y), "ave") > dend <- as.dendrogram(hc) > plot(dend, main="CA 2004 Election Results by County") > > An example of the output can be found here: > http://casoilresource.lawr.ucdavis.edu/drupal/node/206?size=_original > > > I have experimented with the edgePar and nodePar parameters for the > plot.dendrogram() method, but have not been able to make sense of the > output. > > The basis for setting the colors of the leaves in the dendrogram is a > simple majority calculation: > > reds <- y[y$B > 0.5, ] > blues <- y[y$K > 0.5, ] > > Such that leaves in the tree will be colored based on the membership > in either of the two above groups. > > Is there a resource documenting how this might be accomplished? > > Any thoughts or ideas would be greatly appreciated. > > Cheers, > > DylanReplying to my own post... Discovered the dendapply() function: reds <<- as.factor(row.names(y[y$B > 0.5, ])) blues <<- as.factor(row.names(y[y$K > 0.5, ])) #define a function for coloring and sizing node elements: colLab <- function(n) { if(is.leaf(n)) { a <- attributes(n) if ( length(which(blues == a$label)) == 1 ) { attr(n, "nodePar") <- c(a$nodePar, list(lab.col = "blue", lab.cex=.7, col="blue", cex=pop[n], pch=16 )) } else { attr(n, "nodePar") <- c(a$nodePar, list(lab.col = "red", lab.cex=.7, col="red", cex=pop[n], pch=16)) } } n } #modfiy dendrogram nodes and re-plot dend_colored <- dendrapply(dend, colLab) ...which did the trick http://casoilresource.lawr.ucdavis.edu/drupal/node/210 -- Dylan Beaudette Soils and Biogeochemistry Graduate Group University of California at Davis 530.754.7341 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Gabor Grothendieck
2006-Mar-11 14:56 UTC
[R] coloring leaves in a hclust or dendrogram plot [solved]
Where does one find A2Rplot that is called in that example? A2R does not appear to be on CRAN. I did find this: http://addictedtor.free.fr/Download/A2R.zip which is a zip file containing some R code but it is not a package, which the line library(A2R) seems to need, and it does not include A2Rplot in any case. On 3/10/06, Christos Hatzis <christos at silicoinsights.com> wrote:> See also the colored dendrogram in the R Graph Gallery. It has a couple of > additional cool features. > > http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=79 > > > -Christos Hatzis > > -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Dylan Beaudette > Sent: Friday, March 10, 2006 4:37 PM > To: r-help at stat.math.ethz.ch > Subject: Re: [R] coloring leaves in a hclust or dendrogram plot [solved] > > On Thursday 09 March 2006 06:12 pm, Dylan Beaudette wrote: > > Greetings, > > > > I have perused the r-help mailing list archives for an answer to this > > question, without avail. > > > > I would like to color the "leaves" of a dendrogram plot based on a > > cutoff in one of the variables involved in the initial clustering. > > > > My input data is in the form of: > > B K > > Alameda 0.2475770 0.7524230 > > Alpine 0.4546784 0.5453216 > > Amador 0.6278610 0.3721390 > > > > essentially rows labeled by county name, with two variables: percent > > voted for B and percent voted for K. While it is obvious that this is > > somewhat of a contrived example, I intend to use this as a learning > device. > > > > Here is the code used to create and plot the dendrogram: > > hc <- hclust(dist(y), "ave") > > dend <- as.dendrogram(hc) > > plot(dend, main="CA 2004 Election Results by County") > > > > An example of the output can be found here: > > http://casoilresource.lawr.ucdavis.edu/drupal/node/206?size=_original > > > > > > I have experimented with the edgePar and nodePar parameters for the > > plot.dendrogram() method, but have not been able to make sense of the > > output. > > > > The basis for setting the colors of the leaves in the dendrogram is a > > simple majority calculation: > > > > reds <- y[y$B > 0.5, ] > > blues <- y[y$K > 0.5, ] > > > > Such that leaves in the tree will be colored based on the membership > > in either of the two above groups. > > > > Is there a resource documenting how this might be accomplished? > > > > Any thoughts or ideas would be greatly appreciated. > > > > Cheers, > > > > Dylan > > Replying to my own post... > > Discovered the dendapply() function: > > reds <<- as.factor(row.names(y[y$B > 0.5, ])) blues <<- > as.factor(row.names(y[y$K > 0.5, ])) > > #define a function for coloring and sizing node elements: > colLab <- function(n) > { > if(is.leaf(n)) > { > a <- attributes(n) > if ( length(which(blues == a$label)) == 1 ) > { > attr(n, "nodePar") <- c(a$nodePar, list(lab.col = "blue", lab.cex=.7, > col="blue", cex=pop[n], pch=16 )) > } > else > { > attr(n, "nodePar") <- c(a$nodePar, list(lab.col = "red", lab.cex=.7, > col="red", cex=pop[n], pch=16)) > } > } > n > } > > #modfiy dendrogram nodes and re-plot > dend_colored <- dendrapply(dend, colLab) > > ...which did the trick > > http://casoilresource.lawr.ucdavis.edu/drupal/node/210 > > > > -- > Dylan Beaudette > Soils and Biogeochemistry Graduate Group University of California at Davis > 530.754.7341 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >