为什么image()函数不能正常工作? [英] Why does the image() function not work properly?
问题描述
我有一个大的二进制矩阵(0,1),我想要将它可视化,以便我的矩阵的每个条目都是图中的一个像素。
我使用 image()
函数,但它不能按我的预期工作。
我希望如此,因为最后一行条目都是零,那么我应该在图像的最后一行看不到白色像素。
I have big binary matrix (0,1) and I want to visualize it so that every entry of my matrix is one pixel in the plot.
I use the image()
function, but it does not work as I expected.
I would expect that, because the last row entries are all zero, then I should see no white pixels in the last row of the image.
这是我试着用它的输出一个简单的例子:
Here is my try on a simple example with its output:
a <- list(c(0,1,1,0),c(1,1,0,0))
b <- matrix(unlist(a),ncol=2)
> b
[,1] [,2]
[1,] 0 1
[2,] 1 1
[3,] 1 0
[4,] 0 0
推荐答案
作为图片
绘制输入矩阵的逆时针旋转,你必须将其转置然后翻转才能正常绘制:
As the image
plots the counter-clockwise rotation of the input matrix, you have to transpose it and then flip it to be normally plotted:
tb <- t(b)
ftb <- tb[ , ncol(tb):1]
现在你可以使用自定义颜色,如果你不喜欢默认颜色,例如0为灰色,1为红色。同样使用等于1的 asp
会使图像的每个像素成矩形(来自@ koekenbakker的评论)。
Now you can use custom colors if you don't like the default ones, e.g. 0 is grey and 1 is red. Also using the asp
equal to one makes each pixel of the image rectangular (from @koekenbakker's comment ).
image(x=1:2, y=1:4, ftb, col = c("grey", "red"), asp = 1)
这篇关于为什么image()函数不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!