简体   繁体   English

R中的热图如何调整列标签的大小?

[英]heatmap in R how to resize columns labels?

I have a data.matrix that is approximately 4000 rows and 100 columns.我有一个大约 4000 行和 100 列的 data.matrix。 I am doing a heatmap of the data like:我正在做数据的热图,例如:

data<-heatmap(data_matrix,Rowv=NA,Colv=NA,col=cm.colors(256),scale="column",margins=c(5,10))

But the problem that I got is that the labels that appear in the column are too grouped, so it is impossible to visualize them correctly.但是我遇到的问题是列中出现的标签过于分组,因此无法正确可视化它们。 How I can resize the heatmap so I can see the values of the labels of the column?如何调整热图的大小,以便查看列标签的值? I tried to print it in pdf, but it only appears a black stripe.我试图用pdf打印它,但它只出现一条黑色条纹。 Thanks谢谢

I am including a figure of the heatmap, the portion that I want to see are the labels that are in the right part, but they are too close together.我包括了一个热图图,我想看到的部分是位于正确部分的标签,但它们靠得太近了。在此处输入图片说明

First of all it's better to put your output directly to a PDF file - you may use other image formats but PDF is the best because it is a vector output and you can zoom as much as you want:首先,最好将您的输出直接放入 PDF 文件 - 您可以使用其他图像格式,但 PDF 是最好的,因为它是矢量输出,您可以根据需要进行缩放:

pdf("Your-file.pdf", paper="a4", width=8, height=8)

Then it's better to use pheatmap ( = pretty heatmap) package.那么最好使用pheatmap (=漂亮的热图)包。 It makes really better heatmaps with a color key besides your heatmap.除了您的热图外,它还可以使用颜色键制作更好的热图。 Finally although the pheatmap() function tries to reduce the label size while you have many rows, but it fails for really large number of rows.最后,尽管pheatmap()函数尝试在您有很多行时减小标签大小,但是对于非常多的行却失败了。 So I use the code below for really high - but not too high - number of rows:因此,我将下面的代码用于非常高 - 但不是太高 - 的行数:

library(pheatmap)   
library(gplots)
if (nrow(table) > 100) stop("Too many rows for heatmap, who can read?!")
fontsize_row = 10 - nrow(table) / 15
pheatmap(table, col=greenred(256), main="My Heatmap", cluster_cols=F, 
         fontsize_row=fontsize_row, border_color=NA)

You may change fontsize_col for the column labels.您可以更改列标签的fontsize_col You have many interesting options like display_numbers to have the values inside the cells of your heatmap.您有许多有趣的选项,例如display_numbers ,可以在热图的单元格内设置值。 Just read ?pheatmap .只需阅读?pheatmap

This is an example generated by the default parameters of pheatmap() command:这是pheatmap()命令的默认参数生成的示例:

在此处输入图片说明

Finally note that too many rows are easy to read on a display, but useless for print.最后请注意,太多的行在显示器上很容易阅读,但对打印毫无用处。

In Rstudio you can easily resize the graphic window, same holds for Rgui.在 Rstudio 中,您可以轻松调整图形窗口的大小,Rgui 也是如此。 Alternatively, if you save the plot to file you can use a bigger size for your graphics, eg bigger width and height when calling pdf or png .或者,如果您将绘图保存到文件,您可以为您的图形使用更大的尺寸,例如在调用pdfpng时更大的widthheight

You can use cexRow = and cexCol =.您可以使用 cexRow = 和 cexCol =。

You can get more information into ??heatmap.2您可以在??heatmap.2 中获取更多信息

# Row/Column Labeling
margins = c(5, 5),
ColSideColors,
RowSideColors,
cexRow = 0.2 + 1/log10(nr),
cexCol = 0.2 + 1/log10(nc),
labRow = NULL,
labCol = NULL,
srtRow = NULL,
srtCol = NULL,
adjRow = c(0,NA),
adjCol = c(NA,0),
offsetRow = 0.5,
offsetCol = 0.5,
colRow = NULL,
colCol = NULL

If you use pheatmap ( https://www.rdocumentation.org/packages/COMPASS/versions/1.10.2/topics/pheatmap ) you can spread out those labels by adjusting the cellheight parameter.如果您使用pheatmap ( https://www.rdocumentation.org/packages/COMPASS/versions/1.10.2/topics/pheatmap ),您可以通过调整cellheight参数来展开这些标签。

If you are doing this in R notebook, even though the entire heat map will not display in your output window when you run the code, when you save the heat map to your computer using the filename parameter, pheatmap will automatically calculate the optimal size for the output file so that your entire heatmap will be displayed in your output file.如果您在 R notebook 中执行此操作,即使在运行代码时整个热图不会显示在您的输出窗口中,当您使用filename参数将热图保存到您的计算机时,pheatmap 将自动计算最佳大小输出文件,以便您的整个热图将显示在您的输出文件中。 If this size is not to your liking you can adjust using width and height parameters, but it is unlikely you will want to do this.如果此尺寸不符合您的喜好,您可以使用widthheight参数进行调整,但您不太可能想要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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