Plot heatmap using row_splitting but splitting should based on a column in dataset - ComplexHeatmap
0
0
Entering edit mode
13 months ago
TJay • 0

I have a dataset with following format.

  X        M1.    M2.     ...    new_cell_type
cac1.       1.5        1      ...     T
cac2.       1.4      1.4    ...      M
cac3.       0.5      1.1    ...      T
cac4.       0.1      1      ...      N

I want to plot a heatmap based on the above data where rows of the heatmap as cells with cell grouping(with cell_type) and columns as M1,M2,M3... I tried to split the cells with cell type pass that as a matrix to plot heatmap using ComplexHeatmap. But it doesn't work. Any suggestions for this?

data_c <- read.csv('./output/a.csv')
cell_type <- read.csv(file = 'b.csv')
merged_data <- merge(data_c, cell_type,by = "X")

df_list <- split(merged_data, merged_data$new_cell_type)
df_list <- as.matrix(df_list)

heat_col <- colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))

hm <- Heatmap( df_list, 
    col = heat_col, 
    row_split = merged_data$new_cell_type, 
    show_row_names = TRUE,
    cluster_rows = TRUE, 
    cluster_columns = TRUE,
   )
  draw(hm)

enter image description here

I'm expecting this kind of heatmap where rows should be cells and left bar is representing celltype. columns should be M1,M2,M3, etc.

R row_split ComplexHeatmap • 855 views
ADD COMMENT
0
Entering edit mode

Instead of splitting your dataset you want to make an annotation object via rowAnnotation and then pass it to the left_annotation argument.

ADD REPLY
0
Entering edit mode

Any guidance to make annotation object?

ADD REPLY
1
Entering edit mode

This is all detailed in the vignettes for ComplexHeatmap. You should read through them first and come back with any specific questions you may still have afterwards.

ADD REPLY
0
Entering edit mode

Yes. I went through that and changed my code as following. Still it's not giving the correct output.

UPDATE.

 data_c <- read.csv('./output/a.csv')
 cell_type <- read.csv(file = 'b.csv')

 merged_data <- merge(data_c, cell_type,by = "X")
 mat <- data.matrix(merged_data[, 2:(ncol(df)-1)])
 rownames(mat) <- merged_data$X
 cell_types <- factor(merged_data$new_cell_type, levels=c("B", "DC", "E","Myel", "NK", "Normoblast","Prog", "T"))
 df_cell_types <- data.frame(cell_type = cell_types)

row_anno <- HeatmapAnnotation(
#df = data.frame(cell_type = cell_types),
df = df_cell_types,  
#width = unit(1, "cm"),
#height = unit(0.5, "cm"),
col = list(cell_type = c(B = "#E41A1C", DC = "#377EB8", E = "#4DAF4A", Myel = "#E41A1C", NK = "#377EB8", Normoblast = "#4DAF4A",Prog ="#E41A1C", T= "#377EB8")),
#show_legend = TRUE
which = "row"
)

hm <- Heatmap(mat, 
name = "Expression", 
col = colorRamp2(c(-1, 0, 1), c("blue", "white", "red")),
show_row_names = TRUE,
#clustering_distance_rows = "euclidean",
#clustering_distance_columns = "euclidean",
#row_dend_reorder = TRUE,
row_title = "Cells",
column_title = "Modules",
right_annotation = row_anno
)

draw(hm)

enter image description here

ADD REPLY
0
Entering edit mode

First, set show_row_names = FALSE. You might want to play around with the col = col_fun portion of the vignette to change your scale to be something that more closely represents your data.

ADD REPLY

Login before adding your answer.

Traffic: 2279 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