talgalili / dendextend

Extending R's Dendrogram Functionality
152 stars 28 forks source link

[QUESTION] Providing more horizontal room for the labels in tanglegram #118

Closed mooibroekd closed 4 months ago

mooibroekd commented 4 months ago

When plotting a tanglegram() to compare two dendrograms my labels are not fully showing. I am aware that I can adjust the font size, but that would make the labels more difficult to read. I would rather have a way to expand the space for the labels. I could not find an option to do so when making the plot.

Is it possible to extend the space used by the labels through an option?

################################################################################
## Dendrogram a
################################################################################
a <- list()  # initialize empty object
# define merging pattern: 
#    negative numbers are leaves, 
#    positive are merged clusters (defined by row number in $merge)
a$merge <- matrix(c(-1, -2,
                    -3, -4,
                    1,  2), nc=2, byrow=TRUE ) 
a$height <- c(1, 1.5, 3)    # define merge heights
a$order <- 1:4              # order of leaves(trivial if hand-entered)
a$labels <- c("AAAAAAA",
              "BBBBBBB",
              "CCCCCCC",
              "DDDDDDD")    # labels of leaves
class(a) <- "hclust"        # make it an hclust object

#convert to a dendrogram object if needed
ad <- as.dendrogram(a) |> 
  dendextend::set("labels_to_char") # make sure the labels are characters

# dendrogam b
bd <- ad
################################################################################
## Combine using match_order_by_labels
################################################################################

# no warning message:
ab <- dendextend::dendlist(ad,
                           bd) |>
  dendextend::tanglegram()

Created on 2024-04-20 with reprex v2.1.0

mooibroekd commented 4 months ago

The tanglegram() function has the parameters left_dendo_mar and right_dendo_mar. These use the defined margins like margin_inner. When this variable is increased from the default parameter, it will show the labels without overlapping. In the reprex below the default value of margin_inner = 3 is replaced by margin_inner = 5 and this shows the labels nicely.

################################################################################
## Dendrogram a
################################################################################
a <- list()  # initialize empty object
# define merging pattern: 
#    negative numbers are leaves, 
#    positive are merged clusters (defined by row number in $merge)
a$merge <- matrix(c(-1, -2,
                    -3, -4,
                    1,  2), nc=2, byrow=TRUE ) 
a$height <- c(1, 1.5, 3)    # define merge heights
a$order <- 1:4              # order of leaves(trivial if hand-entered)
a$labels <- c("AAAAAAA",
              "BBBBBBB",
              "CCCCCCC",
              "DDDDDDD")    # labels of leaves
class(a) <- "hclust"        # make it an hclust object

#convert to a dendrogram object if needed
ad <- as.dendrogram(a) |> 
  dendextend::set("labels_to_char") # make sure the labels are characters

# dendrogam b
bd <- ad
################################################################################
## Combine using match_order_by_labels
################################################################################

# no warning message:
ab <- dendextend::dendlist(ad,
                           bd) |>
  dendextend::tanglegram(margin_inner = 5)

Created on 2024-04-26 with reprex v2.1.0

I do think the documentation might benefit from giving some more information about the use of margins. Additionally, the lab.cex parameter can be used to increase the font size of the labels.

talgalili commented 3 months ago

Just to say thanks for closing this while giving your answer.