Search code examples
rggplot2dendrogramgeom-textgeom-point

My point geometry overwrites my label geometry in my dendrogram


I am trying to create a dendrogram that conveys separate information with the label and geometry points. However, the code that I have written to make the aesthetic for the points at the end of the branch overwrites the code for the aesthetic for the label. How can I convey both aesthetics in the same plot?

Here is the base code for the plot:

p2 <- ggplot(segment(dendro_data(nt.agna.dend))) +
  geom_segment(aes(x=x, y=y, xend=xend, yend=yend))

Where nt.agna.data is the data for the dendrogram

Here is the coding for the text

p3 <- p2 + geom_text(data=label(dendro_data(nt.agna.dend)),
               aes(label=label, x=x, y=y, colour=n_labs$group),
               hjust = 1.5, angle = 90, size = 2.3) 

Where n_labs contains the information for the labelling of the dendrogram points.

This creates a plot like this, with the labels arranged by colour However, when I include the data for the point aesthetics, the text aesthetics get overwritten:

p4 <- p3 + geom_point(data=label(dendro_data(nt.agna.dend)),
                     aes(x=x,y=y,shape=n_labs$factor, colour=n_labs$factor),
                     size = 2.3) +
           scale_shape_manual(values=c(20,4)) +
           scale_colour_manual(values=c("PRE"="red","NOT"="black"))

Creating a plot like this, where the points have aesthetics, but the labels do not. How can I convey both pieces of aesthetic information in the same plot, without one overwriting the other?, What am I doing wrong?


Solution

  • To color both your labels and your points you have to create an appropriate color palettes which includes both the colors you want to assign to the groups of labels and the colors for the point categories. As is your color palette only assigns colors to the categories of nlabs$factor. As a result your labels are assigned the na.value=.

    Using some fake example data based on the default example from the ggdendro Getting Started vignette:

    library(ggdendro)
    library(ggplot2)
    
    set.seed(123)
    
    model <- hclust(dist(USArrests), "ave")
    nt.agna.dend <- as.dendrogram(model)
    
    n_labs <- label(dendro_data(nt.agna.dend))
    n_labs$group <- sample(LETTERS[1:9], nrow(n_labs), replace = TRUE)
    n_labs$factor <- sample(c("PRE", "NOT"), nrow(n_labs), replace = TRUE)
    
    # Create color palette
    pal_color <- scales::hue_pal()(length(unique(n_labs$group)))
    names(pal_color) <- unique(n_labs$group)
    pal_color <- c(pal_color, c("PRE" = "red", "NOT" = "black"))
    
    ggplot(segment(dendro_data(nt.agna.dend))) +
      geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
      geom_text(
        data = label(dendro_data(nt.agna.dend)),
        aes(label = label, x = x, y = y, colour = n_labs$group),
        hjust = 1, angle = 90, size = 2.3, nudge_y = -2
      ) +
      geom_point(
        data = label(dendro_data(nt.agna.dend)),
        aes(x = x, y = y, shape = n_labs$factor, colour = n_labs$factor),
        size = 2.3
      ) +
      scale_shape_manual(values = c(20, 4)) +
      scale_colour_manual(values = pal_color) +
      scale_y_continuous(expand = expansion(mult = .05, add = c(20, 0)))