Hi,
Thanks for the answer.
It looks like that the mutate what I was missing so long..

That's my current attempt

Data<-matrix(data=rnorm(900,80,20),nrow=30,ncol=30)
Lengths<- 15
library(reshape2)
library(ggplot2)
require('plyr')
tdm <- melt(Data)
tdm <- mutate(tdm, col = cut(value, seq(15, 90, by=5), labels = seq(20, 90, 
by=5)) )
test<-colorRampPalette(c("orange", "red"),space="Lab",interpolate="linear")
ggplot(tdm, aes(x = Var1, y = Var2, fill = col)) +
  geom_raster() +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  labs(x = "MHz", y = "Threshold", fill = "Duty Cycle")


Two things.
a. I have tried to make a color pallete that goes from orange through all the 
orangish and redish hues to the red but as you can see that failed.
b. One think is that the colors visible in the legend in the right part are not 
always visible. If for example there is a 20 value the 20 will appear and if 
there is not but there is a 25 the bar will start from the 25 onwards.

I would like to thank everyone for their on going support

Regards
Alex


________________________________
 From: Dennis Murphy <djmu...@gmail.com>

Sent: Monday, March 25, 2013 9:17 PM
Subject: Re: [R] Plot Matrix with Data

Hi Alex:

Here are a couple of raw examples, one with a 'discretized' color
gradient and another with ordered factor levels in groups of length
10. Starting with tdm,

library(plyr)
library(ggplot2)
tdm <- mutate(tdm, col = cut(value, seq(0, 140, by = 10),
                              labels = seq(10, 140, by = 10)) )
## or equivalently,
# tdm$col <- cut(col$value, seq(0, 140, by = 10),
#                      labels = seq(10, 140, by = 10)) )

# The numeric factor levels are 1 - 14, so if we convert col to numeric,
# should multiply the values by 10. This plot uses a continuous
# color gradient whose midpoint is near the median value.
ggplot(tdm, aes(x = Var1, y = Var2, fill = 10 * as.numeric(col))) +
       geom_raster() +
       scale_x_continuous(expand = c(0, 0)) +
       scale_y_continuous(expand = c(0, 0)) +
       scale_fill_gradient2(low = "green", mid = "orange",
                            high = "yellow", midpoint = 85) +
       labs(x = "MHz", y = "Threshold", fill = "Duty Cycle")

# This one uses col as a factor. The labels are the correct
# upper limits of each value bin. Obviously you want to use
# a smarter mechanism for assigning colors than the
# default. The colorspace package or the colorRampPalette()
# function might be useful here.
ggplot(tdm, aes(x = Var1, y = Var2, fill = col)) +
  geom_raster() +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  labs(x = "MHz", y = "Threshold", fill = "Duty Cycle")

The values on the x-y axes are the row and column numbers of the
reshaped matrix. You may need to map them to values of the MHz and
Threshold values; I doubt that [0, 1] is the range of each variable
(as you have in another post using base graphics).

Dennis


> Hi ,
> I would like to use ggplot2 to plot a matrix as an image.
>
> You can copy paste the following
>
>
> Data<-matrix(data=rnorm(900,80,20),nrow=30,ncol=30)
>
> lengthOut<-5
>
>
>
> Lengths<- 15
> library(reshape2)
> library(ggplot2)
> tdm <- melt(Data)
>
>
>
>
>
> ggplot(tdm, aes(x = Var2, y = Var1, fill = 
> factor(value)),levels=seq(0,1,by=0.1)) +
>                   labs(x = "MHz", y = "Threshold", fill = "Duty Cycle") +
>                   geom_raster(alpha=1) +
>                   
>scale_fill_discrete(h.start=1,breaks=c(20,30,40,50,60,70,80,90),labels=c(20,30,40,50,60,70,80,90),fill="red")
> +
>                   scale_x_continuous(expand = c(0, 0)) +
>                   scale_y_continuous(expand = c(0, 0))
>
>
>
>
>
>
> # End of code part
>
>
>
> What I wanted to do is to print the values
>
> below 20, with an x color
> between 20-30, with a y clor
> between 30-40, with a z color
> and so on....
>
> I would not care now for the color palette.
> Any would do.
> Could you please help me with that?
>
> Regards
> A
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to