[Rd] A "bug" in plot.dendrogram - can't plot lty with character color

Tal Galili tal.galili at gmail.com
Mon May 18 22:01:44 CEST 2015


The problem:
===========
Once a dendrogram has a branch with both a line type AND a color (which is
a character color), the plot.dendrogram function will not plot and return
an error.

I say this is a bug because (I believe), we would like a dendrogram to be
able to use character colors, while also allowing control over line types.



This e-mail includes an example, and what I think a solution might be.

Reproducible example:
=================

install.packages('dendextend')
library('dendextend')

dend <- 1:2 %>% dist %>% hclust %>% as.dendrogram
plot(dend) # works fine

dend %>% set("branches_lty", 1:2) %>% plot # works fine
dend %>% set("branches_col", 1:2) %>% plot # works fine
dend %>% set("branches_col", as.character(1:2)) %>% plot # works fine
# Fails:
dend %>% set("branches_lty", 1:2) %>% set("branches_col",
as.character(1:2)) %>% plot

### Error:
# Error in segments(x0, y0, x1, y1, col = col, lty = lty, lwd = lwd) :
#    invalid line type: must be length 2, 4, 6 or 8

# This is because edgePar has to hold both "lty" and "col"
# Since "col" is a character, it forces "lty" to become a character also.
dend %>% set("branches_lty", 1:2) %>% set("branches_col",
as.character(1:2)) %>%
   unclass %>% str



Possible solution
==============
The simplest (and backward) compatible solution I can think of is to edit
the function:
stats:::plotNode

And change the following lines:
         col <- Xtract("col", ePar, default = par("col"),
                       i)
         lty <- Xtract("lty", ePar, default = par("lty"),
                       i)
         lwd <- Xtract("lwd", ePar, default = par("lwd"),
                       i)

With:

         col <- Xtract("col", ePar, default = par("col"),
                       i)
         lty <- as.numeric(Xtract("lty", ePar, default = par("lty"),
                       i))
         lwd <- as.numeric(Xtract("lwd", ePar, default = par("lwd"),
                       i))




With regards,
Tal

	[[alternative HTML version deleted]]



More information about the R-devel mailing list