David J. States
2010-Apr-17 13:35 UTC
[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@uth.tmc.edu<mailto:David.J.States@uth.tmc.edu> URL: http://www.stateslab.org [[alternative HTML version deleted]]
Tal Galili
2010-Apr-18 19:02 UTC
[R] Interacting with dendrogram plots, locator() or click()
Bump, I am curious to know this one as well. p.s: David. there is an interesting article on how to retrieve points from a plot (any plot). I don't think it is the best way for a dendrogam , but in case no one will offer a better solution, this one might work for you: http://www.r-bloggers.com/getting-data-from-an-image-introductory-post/ <http://www.r-bloggers.com/getting-data-from-an-image-introductory-post/> ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Sat, Apr 17, 2010 at 4:35 PM, David J. States <David.J.States@uth.tmc.edu> wrote:> 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@uth.tmc.edu<mailto:David.J.States@uth.tmc.edu> > URL: http://www.stateslab.org > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
David J. States
2010-Apr-19 12:17 UTC
[R] Interacting with dendrogram plots, locator() or click()
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.