0

I was able to correct the error I was getting now I am having trouble with the x-axis labels. They're vertical, not horizontal. Also, the title isn't included in the image, regardless of the size I tried. Suggestions welcomed. Code below as requested.

#Install and load libraries
if (!requireNamespace("BiocManager", quietly = TRUE)) {
  install.packages("BiocManager")
}
BiocManager::install("ComplexHeatmap")
install.packages("openxlsx")

library(ComplexHeatmap)
library(openxlsx)
library(circlize)
library(grid)

#Read excel file
xxx_data <- read.xlsx("xxx_DEgenes.xlsx", sheet = "xxx_Rel_Data")

#Set gene column as row name
rownames(xxx_data) <- xxx_data$gene
xxx_data_matrix <- as.matrix(xxx_data[, c("logFC_15min", "logFC_30min", "logFC_45min", "logFC_75min")])

#Rename matrix columns
colnames(xxx_data_matrix) <- c("logFC 15min", "logFC 30min", "logFC 45min", "logFC 75min")

#QC data is correctly loaded and transformed
print(str(xxx_data_matrix))

#Replace NA values with 0
xxx_data_matrix[is.na(xxx_data_matrix)] <- 0

#Create color mapping
breaks = c(-8, 0, 8)
colors = c("darkblue", "white", "red")

#Generate and save heatmap with adjusted width and height
png("xxx_heatmap.png", width = 3000, height = 2000)

#Create custom features
heatmap_plot <- Heatmap(are_data_matrix, 
                        col = colors,
                        name = "logFC ",
                        show_row_names = TRUE,
                        row_names_gp = gpar(fontsize = 12),
                        column_names_gp = gpar(fontsize = 12),
                        height = nrow(xxx_data_matrix) * 15.5,
                        width = ncol(xxx_data_matrix) * 0.9  #Adjust column width
)

# Print the heatmap
draw(heatmap_plot, annotation_legend_side = "bot")

# Add title back with adjusted positioning
grid.text("Differential Gene Expression in xxx Tomato", x = 0.5, y = 1.1, gp = gpar(fontface = "bold", fontsize = 14))

dev.off()
Nina
  • 1
  • 3
  • 1
    Please do not post pictures of code, rather copy and paste the code as part of your question. – Phil Aug 11 '23 at 19:31
  • 1
    Offhand, check the length of the `colors` vector and the `color_breaks` vector. They should be the same. – Phil Aug 11 '23 at 19:32
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064) – MrFlick Aug 11 '23 at 19:46
  • Thank you, Phil and MrFlick for your input. I have added a reproducible example of my code and updated the problem I am having creating this heatmap. I appreciate your time and feedback. – Nina Aug 13 '23 at 19:30

0 Answers0