R – 像素matrix的图像?

你如何从R中的matrix形成图像?

matrix值将对应于图像上的像素亮度(虽然我现在只对0,1的值感兴趣,但白色或黑色),列和行号对应图像上的垂直和水平位置。

通过制作一个图像我的意思是显示在屏幕上,并保存为JPG格式。

您可以使用'图像'在屏幕上显示它最简单:

m = matrix(runif(100),10,10) image(m) 

请注意,有轴和边距。 如果你想在每个matrix单元格中使用单个图像像素来制作一个jpeg或者png,那么最简单的事情可能就是光栅包中的东西了。

build立一个没有余量的情节:

 par(mar = rep(0, 4)) 

用灰度图像matrix,像间隔人的答案,但完全填充设备:

 m = matrix(runif(100),10,10) image(m, axes = FALSE, col = grey(seq(0, 1, length = 256))) 

用png()调用来创build文件:

 png("simpleIm.png") par(mar = rep(0, 4)) image(m, axes = FALSE, col = grey(seq(0, 1, length = 256))) dev.off() 

如果需要用空间坐标轴(对于X和Y默认为[0,1]),则使用image.default(x, y, z, ...)forms,其中x和y给出z中的像素。 xy可以是dim(z)+ 1的长度,以给出该约定的angular点坐标。

像素中心(这是图像的默认设置):

 x <- seq(0, 1, length = nrow(m)) y <- seq(0, 1, length = ncol(m)) image(x, y, m, col = grey(seq(0, 1, length = 256))) 

像素angular(需要额外的x和y,0现在是左下angular):

 x <- seq(0, 1, length = nrow(m) + 1) y <- seq(0, 1, length = ncol(m) + 1) image(x, y, m, col = grey(seq(0, 1, length = 256))) 

请注意,从R 2.13 image.default获得一个参数useRaster ,它使用非常高效的新graphics函数rasterImage而不是旧的image ,这是有效的多重调用rasterImage在引擎盖下绘制每个像素作为一个多边形。

我做了一个matrix(其中垂直轴增加下降)两种方式之一。 下面是使用heatmap.2()的第一种方法。 它可以更好地控制图中的数值如何格式化(请参阅下面的formatC语句),但是在更改布局时有点难以处理。

  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)) 

在这里输入图像描述

您可以创buildmatrix的热图。

 library(pheatmap) # Create a 10x10 matrix of random numbers m = matrix(runif(100), 10, 10) # Save output to jpeg jpeg("heatmap.jpg") pheatmap(m, cluster_row = FALSE, cluster_col = FALSE, color=gray.colors(2,start=1,end=0)) dev.off() 

请参阅?pheatmap以获取更多选项。

尝试levelplot:

 library(lattice) levelplot(matrix) 

这是第二种方法(再次,垂直轴增加的地方)。 此方法更易于布局,但对图中显示的数值格式的控制较less。

  library(plotrix) #Build the matrix data to look like a correlation matrix n <- 8 x <- matrix(runif(n*n), nrow=n) xmin <- 0 xmax <- 1 for (i in 1:n) x[i, i] <- 1.0 #Make the diagonal all 1's #Generate the palette for the matrix and the legend. Generate labels for the legend palmat <- color.scale(x, c(1, 0.4), c(1, 0.4), c(0.96, 1)) palleg <- color.gradient(c(1, 0.4), c(1, 0.4), c(0.96, 1), nslices=100) lableg <- c(formatC(xmin, format="f", digits=2), formatC(1*(xmax-xmin)/4, format="f", digits=2), formatC(2*(xmax-xmin)/4, format="f", digits=2), formatC(3*(xmax-xmin)/4, format="f", digits=2), formatC(xmax, format="f", digits=2)) #Set up the plot area and plot the matrix par(mar=c(5, 5, 5, 8)) color2D.matplot(x, cellcolors=palmat, main=paste(n, " X ", n, " Matrix Using Color2D.matplot", sep=""), show.values=2, vcol=rgb(0,0,0), axes=FALSE, vcex=0.7) axis(1, at=seq(1, n, 1)-0.5, labels=seq(1, n, 1), tck=-0.01, padj=-1) #In the axis() statement below, note that the labels are decreasing. This is because #the above color2D.matplot() statement has "axes=FALSE" and a normal axis() #statement was used. axis(2, at=seq(1, n, 1)-0.5, labels=seq(n, 1, -1), tck=-0.01, padj=0.7) #Plot the legend pardat <- par() color.legend(pardat$usr[2]+0.5, 0, pardat$usr[2]+1, pardat$usr[2], paste(" ", lableg, sep=""), palleg, align="rb", gradient="y", cex=0.7) 

在这里输入图像描述