[R] Plot a Matrix as an Image with ggplot

Alaios alaios at yahoo.com
Fri Feb 15 08:31:52 CET 2013



Hi,
thanks
I changed slightly the code to be reproducible from everyone . I have  tried ggplot
but I need a bit of help to tweak it a bit

So you can run the following in your computer

  testData<-matrix(data=round(runif(25)),nrow=5,ncol=5,dimnames=list(1:5,1:5))

  p<-ggplot(melt(testData), aes(Var2,Var1, fill=value))+xlab("MHz") + ylab("Threshold") +    geom_raster()


  p


What I want to improve is
a: make the colorbar so only two specific colors lets say black and white and only two values 0 and 1 and somewhere the string as title of the color bar "text" to appear.

I have tried something like
p+   scale_color_gradient(low="red",high="blue")
Fehler in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0


ggplot(melt(testData), aes(Var2,Var1, fill=value,colorbin=2))+xlab("MHz") + ylab("Threshold") +    geom_raster()
but this did not affect colorbar entries.


b. reduce/remove the grayish border that appears between the legend and the image plot

Could you please help me with these two?

Regards
Alex



________________________________
From: John Kane <jrkrideau at inbox.com>
To: Alaios <alaios at yahoo.com>; R help <r-help at r-project.org> 
Sent: Thursday, February 14, 2013 4:35 PM
Subject: RE: [R] Plot a Matrix as an Image with ggplot

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!
Check it out at http://www.inbox.com/marineaquarium



More information about the R-help mailing list