R-像素矩阵的图像?

| 您将如何从R中的矩阵制作图像? 矩阵值将对应于图像上的像素强度(尽管我现在只对0.1值白色或黑色感兴趣。),而列和行号对应于图像上的垂直和水平位置。 通过制作图像,我的意思是将其显示在屏幕上并另存为jpg。     
已邀请:
您可以使用\'image \'最简单地在屏幕上显示它:
m = matrix(runif(100),10,10)
par(mar=c(0, 0, 0, 0))
image(m, useRaster=TRUE, axes=FALSE)
您也可以看一下栅格数据包...     
设置一个没有边距的地块:
par(mar = rep(0, 4))
用灰度对矩阵进行成像,就像spacedman的答案一样,但会完全填充设备:
m = matrix(runif(100),10,10)
image(m, axes = FALSE, col = grey(seq(0, 1, length = 256)))
将其包装在对png()的调用中以创建文件:
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, ...)
形式,其中x和y给出z中像素的中心位置。
x
y
的长度可以为dim(z)+1,以给出该约定的角坐标。 像素中心(这是图像的默认值):
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)))
像素角(需要额外的1个x和y,而0现在是最左下角):
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
,该参数使用非常高效的新图形函数
rasterImage
而不是旧的
image
,后者实际上是多次调用罩下的
rect
以将每个像素绘制为多边形。     
我使用以下两种方法之一做矩阵(垂直轴沿向下方向增加)。以下是使用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))
    
您可以创建矩阵的热图。
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()
有关更多选项,请参见ѭ15。     
试试levelplot:
library(lattice)
levelplot(matrix)
    
这是第二种方法(同样,垂直轴向下增加)。此方法更易于布局,但对绘图中显示的数值格式的控制较少。
 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)
    

要回复问题请先登录注册