[R] Interacting with dendrogram plots, locator() or click()

David J. States David.J.States at uth.tmc.edu
Mon Apr 19 14:17:54 CEST 2010


The two functions below will see if the current graphics device appears to contain a dendrogram plot, and if so will return the substree when a user clicks on a node.  Note: function name identify.dendrogram allows the dendrogram class to bind this function for identify(dnd).  If someone wants to incorporate this into the public dendrogram distribution, please feel free to do so.

David

Example:

# plot a dendrogram of US arrest data
# and print the subtree that a user selects

dnd = as.dendrogram(hclust(dist(USArrests)))
plot(dnd, horiz=TRUE)
dnd2 = identify(dnd)
str(dnd2)

identify.dendrogram = function(dnd) {
#
# Return the dendrogram corresponding to the node a user clicks on.
#

#
# First verify that it is a dendrogram plot corresponding to the
# input dendrogram, and determine if horizontal or veritical
#
        n = attributes(dnd)$members
        h = attributes(dnd)$height
        usr = par()$usr
        dx = usr[1] + usr[2]
        dy = usr[3] + usr[4]
        ok = FALSE
        horiz = FALSE
        if (abs(n + 1 - dx) < 0.001 && abs((h - dy)/dy) < 0.001) {
                ok = TRUE
                horiz = FALSE
        } else {
                if (abs(n + 1 - dy) < 0.001 && abs((h - dx)/dx) < 0.001) {
                        ok = TRUE
                        horiz = TRUE
                }
        }
#
# If the plot matches, call locator() for user input and match the node
#
        if (ok) {
                crd = locator(1)
                if (is.null(crd)) {
                        return(NULL)
                } else {
                        return(find.node(dnd, 1, crd, horiz))
                }
        } else {
                warning("plot that does not correspond to the dendrogram in the call.")
                return(NULL)
        }
}

find.node = function(dnd, offset, crd, horiz) {
#
# find a node in a dendrgram matching the coordinates in crd
# horiz is the plot orientation, see plot(dendrogram)
#
# First see if this node matches the coordinates
#
        h = attributes(dnd)$height
        n = attributes(dnd)$members
        usr = par()$usr

        ok.x = FALSE
        ok.y = FALSE

        if (horiz) {
                ok.x = (abs(crd$x - h) / (usr[1] - usr[2])) < 0.05
                ok.y = round(crd$y,0) >= offset && round(crd$y,0) <= offset + n - 1
        } else {
                ok.y = (abs(crd$y - h) / (usr[4] - usr[3])) < 0.05
                ok.x = round(crd$x,0) >= offset && round(crd$x,0) <= offset + n - 1
        }
        if (ok.x && ok.y) {
                attr = attributes(dnd)
                attr$offset = offset
                attributes(dnd) = attr

                return(dnd)
        }
#
# No, so see if there are children of this node that match
#
        if (!is.leaf(dnd)) {
                nc = length(dnd)
                child.offset = offset
                for (i in 1:nc) {
#
# Return the match subtree or descend further if no match
#
                        ret = find.node(dnd[[i]], child.offset, crd, horiz)

                        if (!is.null(ret)) {
                                return(ret)
                        } else {
                                child.offset = child.offset + attributes(dnd[[i]])$members
                        }
                }
        }
#
# None of the children matched so return NULL
#
        return(NULL)
}


David J. States, M.D. Ph.D
University of Texas Health Science Center at Houston

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of David J. States
Sent: Saturday, April 17, 2010 8:35 AM
To: r-help at R-project.org
Subject: [R] Interacting with dendrogram plots, locator() or click()

I would like to explore dendrogam plots interactively.  For example, click on a node and return information about all of the children of that node.

Is there a high level wrapper for locator() or click() that will return the nearest dendrogram node on a plot?

If not, is there a way to obtain the [x,y] coordinates of all the nodes on a plot?

Thanks,

David

David J. States, M.D., Ph.D.
Professor of Health Information Science
School of Health Information Sciences
Brown Foundation Institute of Molecular Medicine
University of Texas Health Science Center at Houston

Sarofim Research Building Room 437C
1825 Pressler St.
Houston, TX   77030

Telephone: 713 500 3845
email: David.J.States at uth.tmc.edu<mailto:David.J.States at uth.tmc.edu>
URL:  http://www.stateslab.org


	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list