[R] Plot a Matrix as an Image with ggplot

Alaios alaios at yahoo.com
Thu Feb 14 16:15:05 CET 2013

Dear all,
I am trying to plot a matrix I have  as an image

 num [1:21, 1:66] 0 0 0 0 0 0 0 0 0 0 .

 that contains only 0s and 1s,

where the xlabel will be Labeled as 
 num [1:66] 1e+09 1e+09 1e+09 1e+09 1e+09 ...

and the yLabels will be labeled as
 num [1:21] -88 -87 -86 -85 -84 -83 -82 -81 -80 -79 ...

I have found on the internet that I can do something like that with ggplot2.
For example
you can run the following

library(reshape2)library(ggplot2)m =matrix(rnorm(20),5)ggplot(melt(m),aes(Var1,Var2,fill=value))+geom_raster()

What I see missing here is to get my matrix and transform it to a data frame with labels for x and y axis so as ggplot can print the values nice. If you get the idea this matrix will be printed as a two tile pattern let's say with black tiles zeros and white tiles black where a color bar will depicting that black means zero and white one.

To help you with my code you will find attached the three items I have discussed here, the matrix, the x and the y labels.

Could you please help me with that?


More information about the R-help mailing list