[R] Plot a Matrix as an Image with ggplot

Alaios alaios at yahoo.com
Sat Feb 16 00:24:36 CET 2013

```You were right,
the following two work

testData<-matrix(data=round(runif(25)),nrow=5,ncol=5,dimnames=list(Var1=c(1:5),Var2=c(1:5)))
ggplot(melt(testData), aes(Var2,Var1, fill=value))+xlab("MHz") + ylab("Threshold") +    geom_raster() + scale_fill_gradient(low="#FFFFFF", high="#000000")

still there are two minor unresolved issues.

a. How to reduce the extra space in the plot between legend and the tiles?
b. How to make color bar depict only the two values of the game, 0 and 1 , instead of 0, 0.25, 0.50, 0.75, 1?

Regards
Alex

----- Original Message -----
From: Dennis Murphy <djmuser at gmail.com>
To: Alaios <alaios at yahoo.com>
Cc:
Sent: Friday, February 15, 2013 10:19 AM
Subject: Re: [R] Plot a Matrix as an Image with ggplot

scale_fill_gradient and you'll get what you expect.

D.

On Thu, Feb 14, 2013 at 11:31 PM, Alaios <alaios at yahoo.com> wrote:
>
>
> 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
> 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
>
>
> 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
>
>
>> -----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.
>>
>>
>> Alex
>
> ____________________________________________________________
> FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
> Check it out at http://www.inbox.com/marineaquarium
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help