rggplot2heatmapdendrogramggh4x

How to increase the size of the Dendrogram as per with the HeatMap in the exported image in R?


I made a Heatmap and corresponding dendrograms in R ggplot2. However, I'm wondering how can I increase the size of the dendrogram at the exported high-resolution image. Precisely, I want to increase the breadth of both Dendrograms so that the clustering between the elements in the plot can be properly visualized.

Data:

> SampleDat
             CCC          CS        G1          G2          G3        LMS        USC
ASCL2          0  0.00000000 0.0000000  0.00000000  0.19791230  0.0000000 -0.2589399
THRB-AS1       0  0.15478030 0.0000000  0.15665590 -0.05063790  0.0000000 -0.1702256
ZFAND2A        0  0.37089000 0.4538498  0.00000000  0.34598680  0.0000000  0.3016402
C10orf82       0  0.13880730 0.0000000  0.15742300  0.18315680  0.0000000  0.1487810
ADAMTS16       0  0.18127015 0.0000000  0.17522080 -0.15981700  0.0000000  0.0000000
LOC107985287   0 -0.04640695 0.1287075  0.12656770  0.21860815 -0.3203314  0.3492471
LOC105375610   0  0.16883540 0.0000000  0.00000000  0.18512860 -0.3199183  0.0000000
LINC00485      0 -0.17285620 0.3035464 -0.10216190 -0.69079680 -0.1887855  0.0000000
PKMYT1         0  0.00000000 0.0000000  0.00000000  0.00000000 -0.3447935  0.0000000
TBX4           0  0.13699000 0.0000000  0.00000000  0.00000000  0.2159004  0.1126036
CNTN4          0  0.11820680 0.0000000  0.11894160  0.00000000  0.0000000  0.2915814
SUMF1          0  0.00000000 0.0000000  0.00000000  0.00000000 -0.6267713  0.0000000
SLC25A13       0 -0.09982340 0.2151051  0.09800212 -0.26193367  0.0000000 -0.3921427
LOC107984568   0  0.00000000 0.0000000  0.24254940 -0.05271005 -0.4876733  0.0000000
HS3ST2         0  0.14462475 0.0000000  0.10068490  0.00000000  0.1197004  0.1814833
C1orf141       0 -0.23244100 0.0000000  0.11186150 -0.07326740 -0.3609651  0.0000000
VGLL2          0  0.00000000 0.0000000 -0.23984550  0.10843580  0.2534536  0.0000000
REXO2          0  0.00000000 0.0000000  0.00000000 -0.53392505  0.0000000 -0.3302150
PAX6           0  0.22198410 0.0000000  0.19559730  0.00000000  0.2500406  0.0000000
H4C13          0  0.00000000 0.0000000  0.00000000  0.00000000  0.4118989  0.0000000

Script:

library("readr")
library("dplyr")
library("ggh4x")
library("ggsci")
library("ggplot2")

    # Hierarchically cluster AllGrpDMRgene_mat
    SampleDat_yclus <- hclust(dist(SampleDat), "ave")
    SampleDat_xclus <- hclust(dist(t(SampleDat)), "ave")
    
    # Melting SampleDat
    SampleMelt <- data.frame(
      SYMBOL = rownames(SampleDat)[row(SampleDat)],
      Group = colnames(SampleDat)[col(SampleDat)],
      Mdiff = unname(do.call(c, SampleDat))
    )
    
    #mutating a new column for +ve and -ve
    Sample_Plotdat<-SampleMelt %>% 
      mutate(status = case_when(Mdiff < 0 ~ "low",
                                Mdiff > 0  ~ "high"))
    
    #making status as factor
    Sample_Plotdat$status<-as.factor(Sample_Plotdat$status)
    
    # Supply the clustering to the scales
    HeatDenTest<-ggplot(Sample_Plotdat, aes(Group, SYMBOL, fill = status)) +
      geom_raster() +
      xlab("Histological Groups") +
      ylab("Gene Symbol") +
      scale_fill_nejm(labels=c('Negative','Positive' ), name="")+
      scale_y_dendrogram(hclust = SampleDat_yclus) +
      scale_x_dendrogram(hclust = SampleDat_xclus, position = "top")+
      theme(axis.text.y = element_text(size=rel(.3), vjust = 0.5, hjust=1),
            axis.title.y = element_text(size=rel(4)),
            axis.ticks.y = element_blank(),
            axis.text.x = element_text(size=rel(3), vjust = 0.5, hjust=1),
            axis.title.x = element_text(size=rel(4)),
            legend.position = "bottom",
            legend.text=element_text(size=40),
            legend.key.size = unit(3, 'cm'))

 ggsave("HeatDenTest.jpeg", plot = HeatDenTest, width = 18, height =30, units = "in", dpi = 200)

The Dimension of the Original plot is quite large (width = 18, height = 30, units = "in", dpi = 600), because of the size restriction a low-resolution plot is shown here.

Plot: enter image description here


Solution

  • As a first step, I would suggest to stick with the default font sizes set by the theme or set the base font size globally using the base_size= argument of the theme, e.g. theme_grey(base_size = ...). Second, when it comes to exporting you can account for the size of your plot by using e.g. the scale= argument of ggsave(). Finally, when using scale_x/y_dendrogram

    the dendrogram replaces the axis ticks. (see the docs)

    Hence, you can change the appearance of the dendrograms using the theme options for the ticks, e.g. you could increase the size of the dendrograms using axis.ticks.length:

    library("ggh4x")
    library("ggsci")
    library("ggplot2")
    
    HeatDenTest <- ggplot(Sample_Plotdat, aes(Group, SYMBOL, fill = status)) +
      geom_raster() +
      xlab("Histological Groups") +
      ylab("Gene Symbol") +
      scale_fill_nejm(labels = c("Negative", "Positive"), name = "") +
      scale_y_dendrogram(hclust = SampleDat_yclus) +
      scale_x_dendrogram(hclust = SampleDat_xclus, position = "top") +
      theme(
        axis.ticks.length = unit(5, "pt"),
        legend.position = "bottom",
      )
      
    ggsave("HeatDenTest.jpeg",
      plot = HeatDenTest,
      width = 18, height = 30,
      units = "in", dpi = 200,
      scale = .25
    )
    

    enter image description here