Paul Paul - 2 months ago 9
R Question

Converting a matrix into a gray scale image in R

I have the following vector:

vec<-as.vector(rnorm(1:924))


And would like to reshape it into a matrix of the form 33 X 28. Then convert the matrix into a gray scale pixel like image.

In MATLAB, I know there exists imagesc and imshow to convert the matrix into an image. Is there something similar in R ?

Also, can the mean and maximum be adjusted like in MATLAB for the gray scale intensity?

Answer

You are looking for ?heatmap:

heatmap(matrix(vec,33,28),Rowv=NA,Colv=NA,col=paste("gray",1:99,sep=""))

heatmap

You can adjust the min/max color via the col argument. Look at ?image, ?heat.colors and friends.

Comments