2016-09-28 82 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。通過此評論標誌,所以我可以在一兩天的發現打下了獎金。 –