You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to use ComplexHeatmap to plot this table as a Heatmap, where my categories in the description column show me the genes common to different categories.
For that I would like to create rowAnnotations object. This is what I have so far:
library(ComplexHeatmap)
# creating the matrix and set rownames
df_matrix <- as.matrix(df.heatmap.ego %>% select(-ID, -Description, -geneID))
rownames(df_matrix) <- df.heatmap.ego$geneID
# Scaling the data row-wise (genes)
df_matrix <- t(scale(t(df_matrix)))
# Assign colors for each GO category
go_categories <- factor(df.heatmap.ego$Description)
go_colors <- RColorBrewer::brewer.pal(n = length(levels(go_categories)), "Set3")
names(go_colors) <- levels(go_categories)
# Next, I create a binary matrix of geneID vs. description
# Get unique categories
go_terms <- unique(df.heatmap.ego$Description)
# Initialize an empty matrix
go_binary_matrix <- matrix(0, nrow = nrow(df_matrix), ncol = length(go_terms))
rownames(go_binary_matrix) <- df.heatmap.ego$geneID
colnames(go_binary_matrix) <- go_terms
# Next, I fill the matrix with 1s where a gene is associated with a GO term
for (i in 1:nrow(df.heatmap.ego)) {
gene <- df.heatmap.ego$geneID[i]
category <- df.heatmap.ego$Description[i]
go_binary_matrix[gene, category] <- 1
}
# Create a list of annotation bars for each category
annotations <- lapply(colnames(go_binary_matrix), function(category) {
rowAnnotation(category = go_binary_matrix[, category],
col = list(category = c("0" = "white", "1" = "blue")),
name = category)
})
# Combine all annotations into a single row annotation object
combined_annotations <- HeatmapAnnotation(
df = do.call(cbind, lapply(annotations, function(x) x$category)),
col = list(category = c("0" = "white", "1" = "blue"))
)
# Step 3: Create the heatmap and add the annotations
# First, create an empty heatmap and then add each annotation individually
heatmap_obj <- Heatmap(df_matrix,
name = "Scaled Expression",
row_title = "Genes",
column_title = "Samples")
# Add each annotation to the heatmap
for (annotation in annotations) {
heatmap_obj <- heatmap_obj + annotation
}
# Step 4: Generate the heatmap
draw(heatmap_obj)
But this give me this image:
How do I change it to have the name of the category instead of the literal "category"?
thanks
Assa
The text was updated successfully, but these errors were encountered:
I have the following table of data
I would like to use
ComplexHeatmap
to plot this table as a Heatmap, where my categories in the description column show me the genes common to different categories.For that I would like to create rowAnnotations object. This is what I have so far:
But this give me this image:
How do I change it to have the name of the category instead of the literal "category"?
thanks
Assa
The text was updated successfully, but these errors were encountered: