[R] Plot a Matrix as an Image with ggplot

John Kane jrkrideau at inbox.com
Thu Feb 14 16:35:26 CET 2013


The R-help list is rather picky about what attached. None of your attachments arrived. 

The str() info is useful but please supply some sample data 

 The easiest way to supply data  is to use the dput() function.  Example with your file named "testfile": 
dput(testfile) 

Then copy the output and paste into your email.  For large data sets, you can just supply a representative sample.  Usually, 
dput(head(testfile, 100)) will be sufficient.  

How are you writing the code/or what format is the original email. Your code in the body of the text is badly messed up -- you probably need to post only in text. HTML etc is automatically dropped.


 

John Kane
Kingston ON Canada


> -----Original Message-----
> From: alaios at yahoo.com
> Sent: Thu, 14 Feb 2013 07:15:05 -0800 (PST)
> To: r-help at r-project.org
> Subject: [R] Plot a Matrix as an Image with ggplot
> 
> Dear all,
> I am trying to plot a matrix I have  as an image
> 
> str(matrixToPlot)
>  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
> str(xLabel)
>  num [1:66] 1e+09 1e+09 1e+09 1e+09 1e+09 ...
> 
> and the yLabels will be labeled as
> str(yLabel)
>  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?
> 
> Alex

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!



More information about the R-help mailing list