Plot colors change when exporting as SVG file in R
14:45 26 Jun 2020

I am generating a figure for publishing that includes a heatmap and when I export it as .svg the colors change.

When I visualize it in R the colors look ok (heatmap colors match the legend colors): enter image description here

However, after exporting it to svg the colors in the heatmap look dimmer:

enter image description here

I think it has something to do with the svg device including a "white border" to every row of the heatmap, as the colors in the legend are unaffected. If you zoom in very closely you can appreciate it:

enter image description here

I am using the following code to export them. I have tried changing the bg and antialias parameteres of svg but there is no change:

svg(filename="test.svg",width = 10, height = 5)
Figure3
dev.off()

When I export is as pdf, the colors are ok:

enter image description here

I guess I will export them as .pdf but I would like to know what is causing this more as a curiosity.

Thanks!

Edit: added dataset and code to generate the plot:

From the package documentation, default colors for the heatmap are generated as:

colorRamp2(seq(min(mat), max(mat), length = 3), c("blue", "#EEEEEE", "red"))

For the blocks in the left I used another palette in the initial example, here I use RColorBrewer and it has the same effect.

I dont think it's a problem of the color palette as it is ok in the legend, I guess it's a problem of a "border" around each row (both the blocks and the heatmap colors are defined as rows of a tile).

Dataset:

https://file.io/l6fL28Cy

Code:

library(ComplexHeatmap)
library(RColorBrewer)

Import data:
x = readRDS("data.rds")

#Select number of clusters to plot:
clusters = 7

#Generate dendrogram to make block annotation:
dend = as.dendrogram(hclust(as.dist(1- cor(t(x))),"complete"), hang=-1)

#Create annotation block for heatmap
cl_num = cutree(dend, k = clusters)
cl_col = brewer.pal(clusters,"Set2")
names(cl_col) = unique(cl_num)
cl_col = list(cl_num=cl_col)

row_ha = rowAnnotation(cl_num = cl_num,
                       col = cl_col,
                       annotation_legend_param = list(title = "Cluster"),
                       show_annotation_name = F)
###Create heatmap:
Heatmap(x,
        clustering_distance_columns = "euclidean",
        clustering_method_columns = "complete",
        clustering_distance_rows = "pearson",
        clustering_method_rows = "complete",
        row_split = clusters,
        left_annotation = row_ha,
        show_row_names = F,
        heatmap_legend_param = list(title = "Z-score"),
        column_title = "Treatment",
        row_title = "Genes",
        use_raster = F,
        column_dend_height = unit(0.1, "cm"),
        column_names_gp = gpar(fontsize = 16),
        column_names_rot = 0,
        column_names_centered = TRUE)
r svg ggplot2