简体   繁体   中英

How set Times New Roman text for Rows and Columns in a Heatmap In R

I am making a Heatmap in R using Heatmap.2 and gplot. I want so that all the text, including the row and column names, along with the axis titles, are written in Times New Roman.

Here is the code I have so far:

install.packages("extrafont")
library(extrafont)
font_import()
loadfonts(device="pdf")      
fonts()
heatmap.2(data,Colv=NA, Rowv = NA, scale="row", tracecol = NA, col=terrain.colors(256),     xlab="Treatments", ylab="Genes of Interest", theme_bw(), theme(text=element_text(family="Times New Roman", face="bold", size=12)))

But unfortunately, this only makes the axis titles Times new Roman, nothing else. I was wondering if anybody knew how to make the rows and column titles the same?

Many thanks!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM