'How to save a heat map created using the ComplexHeatmap package to png format?
I have made a heatmap using the ComplexHeatmap package in R. I wanted to save the map on my pc and I run the following code:
df <- read.csv("/home/aahm/filename.csv")
View(df)
df1<- t(df[,2:ncol(df)])
colnames(df1) <- df[,1]
View (df1)
df2 <- scale(df1)
library(ComplexHeatmap)
library(RColorBrewer)
library(circlize)
mycols <- colorRamp2(breaks = c(0, 0.2, 0.4, 0.6, 1.0),
colors = c('white', "green", "red", "magenta", "blue"))
png(file="/home/aahm/Desktop/filename_heatmap.png")
Heatmap(df1, width = unit(15, "cm"),
name = "nucleotide diversity scores",
row_names_gp = gpar(fontsize = 7), column_title = "Nucleotide Positions", row_title = '1000 Genomes populations',
column_title_side = "bottom",
col = mycols,
show_column_dend = FALSE, cluster_rows = FALSE,
column_order = order(as.numeric(gsub("column", "", colnames(df1)))),
column_names_rot = 45,
)
dev.off()
An empty textfile with extension .png is created on my desktop rather than a png file with the map. Can someone help me find out what is wrong with my code?
Solution 1:[1]
The sequence of the code should be like this
png("test.png",width=3.25,height=3.25,units="in",res=1200)
Your heat-map code
dev.off()
UPDATE
Save with your required dimension. You need to try which resolution is best for you.
png("test.png",width=3.25,height=3.25,units="in",res=1200)
Solution 2:[2]
According to ComplexHeatmap guide (https://jokergoo.github.io/ComplexHeatmap-reference/book/a-single-heatmap.html) you just need to save you heatmap as a new variable and then use draw() function to create plot in the pdf file.
png(file="/home/aahm/Desktop/filename_heatmap.png")
ht <- Heatmap(...)
draw(ht)
dev.off()
You may also use some additional arguments of Heatmap function such as use_raster, raster_quality, raster_resize_mat, raster_by_magick, etc.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ann_K |
