你将如何从R中的矩阵中生成图像?
矩阵值将对应于图像上的像素强度(尽管我目前只对0,1值白色或黑色感兴趣。),而列和行数对应于图像上的垂直和水平位置。
通过制作图像,我的意思是在屏幕上显示它,并将其保存为jpg格式。
发布于 2011-04-13 00:35:29
你可以使用‘image’最简单地在屏幕上显示它:
m = matrix(runif(100),10,10)
par(mar=c(0, 0, 0, 0))
image(m, useRaster=TRUE, axes=FALSE)你也可以看看光栅包..。
发布于 2011-04-13 06:56:42
设置不带边距的绘图:
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,将每个像素绘制为多边形。
发布于 2011-04-13 01:11:35
我做一个矩阵(其中垂直轴增加向下)的两种方式之一。下面是使用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))

https://stackoverflow.com/questions/5638462
复制相似问题