It is not clear to me whether you are trying to export a PDF file with the fonts, which should work with this example:
set.seed(1)
mat <- matrix(stats::runif(100, 3, 14), nrow = 10, ncol = 10,
dimnames = list(paste0("Row", 1:10), paste0("Col", 1:10)))
pdf(file="font_plot.pdf", family="Times", width=6, height=6)
gplots::heatmap.2(mat, dendrogram = "none", scale="row", tracecol = NA,
col=terrain.colors(256),
xlab="Treatments", ylab="Genes of Interest")
dev.off()
#> png
#> 2
Created on 2020-06-29 by the reprex package (v0.3.0)
... or whether you want the output to any graphics device (including the screen) to have those fonts.
Note that with heatmap.2
you are not dealing with ggplot2
(but grid
) graphics, therefore theming does not work the way you tried.
You have several options, including approaches using ggplot
(involving geom_tile
), plotly
, or the more configurable grid-based ComplexHeatmap
package. An example for the latter is shown below:
set.seed(1)
mat <- matrix(stats::runif(100, 3, 14), nrow = 10, ncol = 10,
dimnames = list(paste0("Row", 1:10), paste0("Col", 1:10)))
suppressPackageStartupMessages(library(ComplexHeatmap))
ht_opt(heatmap_column_names_gp = gpar(fontfamily = "Times"),
heatmap_row_names_gp = gpar(fontfamily = "Times"),
heatmap_column_title_gp = gpar(fontfamily = "Times"),
heatmap_row_title_gp = gpar(fontfamily = "Times"),
legend_title_gp = gpar(fontfamily = "Times"),
legend_labels_gp = gpar(fontfamily = "Times"))
draw(Heatmap(t(scale(t(mat))),
cluster_columns=FALSE, cluster_rows = FALSE,
heatmap_legend_param = list(title = "Row Z-score",
direction = "horizontal",
title_position = "lefttop"),
column_title="Treatments", column_title_side="bottom",
row_title="Genes of Interest", row_title_side="right",
col=terrain.colors(256)),
heatmap_legend_side = "top")

Created on 2020-06-29 by the reprex package (v0.3.0)