2016-09-28 73 views
1

我有这样的“有色”矩阵R中彩色矩阵R保存乳胶

library(gplots) 

#Build the matrix data to look like a correlation matrix 
x <- matrix(rnorm(64), nrow=8) 
x <- (x - min(x))/(max(x) - min(x)) #Scale the data to be between 0 and 1 
for (i in 1:8) x[i, i] <- 1.0 #Make the diagonal all 1's 

#Format the data for the plot 
xval <- formatC(x, format="f", digits=2) 
pal <- colorRampPalette(c(rgb(0.96,0.96,1), rgb(0.1,0.1,0.9)), space = "rgb") 

#Plot the matrix 
x_hm <- heatmap.2(x, Rowv=FALSE, Colv=FALSE, dendrogram="none", main="8 X 8 Matrix Using Heatmap.2", xlab="Columns", ylab="Rows", col=pal, tracecol="#303030", trace="none", cellnote=xval, notecol="black", notecex=0.8, keysize = 1.5, margins=c(5, 5)) 

,我想将其导入乳胶。我能怎么做? 谢谢你的帮助!

回答

2

gplots::heatmap.2只打印彩色矩阵的图像,它实际上不会返回一个。不过,我们可以重新创建它自己,并将其转换成乳胶表(虽然保存图像和乳胶使用\includegraphics的评论是一样好)。这是您可以使用的.Rmd文件。确保编织成PDF!

--- 
title: "Hey Stack" 
header-includes: 
    - \usepackage[table]{xcolor} 
output: pdf_document 
--- 

```{r, include = FALSE} 
library(gplots) 
library(xtable) 

#Build the matrix data to look like a correlation matrix 
x <- matrix(rnorm(64), nrow=8) 
x <- (x - min(x))/(max(x) - min(x)) #Scale the data to be between 0 and 1 
for (i in 1:8) x[i, i] <- 1.0 #Make the diagonal all 1's 

#Format the data for the plot 
xval <- formatC(x, format="f", digits=2) 
pal <- colorRampPalette(c(rgb(0.96,0.96,1), rgb(0.1,0.1,0.9)), space = "rgb") 

x_hm <- heatmap.2(x, Rowv=FALSE, Colv=FALSE, dendrogram="none", 
    main="8 X 8 Matrix Using Heatmap.2", xlab="Columns", ylab="Rows", col=pal, 
    tracecol="#303030", trace="none", cellnote=xval, notecol="black", notecex=0.8, 
    keysize = 1.5, margins=c(5, 5)) 
``` 

```{r, results = 'asis'} 
color_m <- apply(round(x, 2), 2, function(r) { 
    col <- cut(r, x_hm$breaks, gsub("#", "", x_hm$col), include.lowest = TRUE) 

    paste0("\\cellcolor[HTML]{", col, "}{", r, "}") 
}) 

print(xtable(color_m), sanitize.text.function = identity, comment = FALSE, 
     include.rownames = FALSE, include.colnames = FALSE) 
``` 
+0

希望我可以给这个答案更upvotes。通过此评论标志,所以我可以在一两天的发现打下了奖金。 –