Why does the image() function not work properly?

后端 未结 2 1343
不知归路
不知归路 2021-01-17 07:05

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

相关标签:
2条回答
  • 2021-01-17 07:38

    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]
    

    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)
    

    enter image description here

    0 讨论(0)
  • 2021-01-17 08:00

    I dnt know about the image() function, but you can do this with EBImage package like this.

    a<-list(c(0,1,1,0),c(1,1,0,0))
    b<-matrix(unlist(a),ncol=2)
    library(EBImage)
    x <- as.Image(b)
    display(x,method='raster')
    
    0 讨论(0)
提交回复
热议问题