CompleX heat Map
1
0
Entering edit mode
24 months ago
Sidra • 0

I have this R script for Complex Heat map, code is ok but the problem is the legend size. i am unable to adjust the legend size on the right side of the diagram.

## Install Packages

#install.packages("dplyr")
#install.packages("tidyr")
#install.packages("plyr")
#install.packages("reshape2")
#install.packages("ComplexHeatmap")
#install.packages("ggpubr")
#install.packages("circlize")
#install.packages("RColorBrewer")
#install.packages("scales")
#install.packages("stringdist")
#install.packages("corrplot")
#install.packages("heatmaply")

## Call Libraries
library(dplyr)
library(tidyr)
library(plyr)
library(reshape2)
library(ComplexHeatmap)
library(ggpubr)
library(circlize)
library(RColorBrewer)
library(scales)
library(stringdist)
library(corrplot)
library(heatmaply)


## Calling CSV file of interest and data formating

mydf_orig <- read.csv("plamid_data.csv", header = FALSE)
mydf <- mydf_orig  %>%  mutate_all(trimws)
mydf[,1]
mydf[2,]

rownames(mydf)<-mydf[,1]
colnames(mydf)<-mydf[2,]

mydf<-mydf[-c(1,2),]
mydf2 <- mydf[order(mydf[,2]),]
mydf<-mydf2[,-c(1,2)]
class(mydf)

mat<- as.matrix(mydf)
head(mat)
#write.csv(mat, file="matrix.csv")

## Starting the data manipulation for complex heatmap

# Annotation for Classes.
Antibiotics <- t(mydf_orig[1,-c(1,2)])
Antibiotics

Antibiotics_unique <- t(unique(Antibiotics))
Antibiotics_unique

colourCount_Antibiotics = length(Antibiotics_unique)
colourCount_Antibiotics

getPalette = colorRampPalette(brewer.pal(9, "Set1"))

Antibiotics_colors<- getPalette(colourCount_Antibiotics)
Antibiotics_colors

show_col(Antibiotics_colors)

list_antibiotics_colors <- setNames(Antibiotics_colors, Antibiotics_unique)
list_antibiotics_colors

h_antib = HeatmapAnnotation(Antibiotics = Antibiotics,
                            col = list(Antibiotics= list_antibiotics_colors),
                            gp = gpar( col = rep(2:3, each = 2)),

                            border = TRUE)

plot(h_antib)

## Annotation for Locations.

Cities <- (mydf2[,2])
Cities

Cities_unique <- t(unique(Cities))
Cities_unique

colourCount_Cities = length(Cities_unique)
colourCount_Cities

getPalette2 = colorRampPalette(brewer.pal(9, "Set3"))
colors_Cities<- getPalette2(colourCount_Cities)
colors_Cities

show_col(colors_Cities)

col2_cities <- setNames(colors_Cities, Cities_unique)

h_cities= rowAnnotation(Cities = Cities,
                        col = list(Cities= col2_cities),
                        gp = gpar(fontsize = 3),
                        border = TRUE)

plot(h_cities)

## Generation of colors for conditions.

condition_colors <- c("#36D3C4","#D33636")

condition_colors_list = structure(condition_colors, names = c("1","0"))
condition_colors_list
show_col(condition_colors_list)

haha <- setNames(condition_colors, c("1","0"))
haha
show_col(haha)

## Generation of dendogram for character data.

# For calculating distances using stringdist package
dist_letters = function(x, y) {
  x = strtoi(charToRaw(paste(x, collapse = "")), base = 16)
  y = strtoi(charToRaw(paste(y, collapse = "")), base = 16)
  sqrt(sum((x - y)^2))
}

fA =c("A","A","B")
fB=c("A","A","B")
dist_letters(fA,fB)

fA=c("A","A","A")
fB =c("Z","Z","Z")
dist_letters(fA,fB)

#between 1st and 2nd row
dist_letters(mat[1,],mat[2,])

#between 1st and 2nd column
dist_letters(mat[,1],mat[,2])

## Calculation of frequencies of R, I and S.

table(mydf[,1],useNA = "ifany")
lapply(mydf, table)

factors <- sort(unique(unlist(mydf)))
factors

total_IRS<- as.data.frame(do.call(rbind, lapply(mydf, function(x) table(factor(x, levels=factors)))))
total_IRS

total <- tibble::rownames_to_column(total_IRS, "Antibiotic")
total

total_t<- t(total)
total_t

colnames(total_t)<-total_t[1,]
total_t <- total_t[-1,]
total_t

class(total_t) <- "numeric"

## Annotation of Frequency Plot for Heatmap

h_frequencies = HeatmapAnnotation(Frequencies=anno_barplot(t(total_t),
                                                           gp=gpar(fill = condition_colors, 
                                                                   col = condition_colors)))
plot(h_frequencies)

## Generation of Heatmap

Heatmap(mat, name = "Condition",
        border_gp = gpar(col = "black", lty = 2),
        clustering_method_rows = "single",
        column_title = "Heatmap",
        column_title_gp = gpar(fontsize =5, fontface = "bold"),
        col = condition_colors_list,
        row_title = "Isolates",
        bottom_annotation = h_antib ,
        top_annotation = h_frequencies,
        right_annotation =h_cities,
        column_km = 1,
        row_names_side = "left",
        row_names_gp = gpar(fontsize = 5),
        show_row_names = FALSE,
        column_names_rot = 1,
        column_names_gp = gpar(fontsize = 5),
        heatmap_height = unit(0.25, "mm")*nrow(mat),
        #clustering_distance_rows = dist_letters,
        clustering_distance_columns = dist_letters,
        row_dend_reorder = FALSE,
        column_dend_reorder = FALSE,
        heatmap_width = unit(1.5, "mm")*ncol(mat),
)

enter image description here

ComplexHeatmap • 1.0k views
ADD COMMENT
0
Entering edit mode
24 months ago
bkleiboeker ▴ 370

You can adjust legend width, legend height, and legend fontsize with the following 3 arguments to heatmap_legend_param

h <- Heatmap(...,
         heatmap_legend_param = list(legend_width = unit(7, "cm"),
                                     legend_height = unit(4, "cm"),
                                     labels_gp = gpar(fontsize = 2)))

And can adjust legend location with:

draw(h, heatmap_legend_side = "bottom")

This package always works better for me when I make a ComplexHeatmap object (here, h) and then call it in a separate draw() function.

ADD COMMENT

Login before adding your answer.

Traffic: 2213 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6