Hi,
it was very kind of you to help me again.
Your code works, and the reason I was using the limits as that is that my
dataset is slightly different than the one I used for showing the problem here.
More specifically this is my dataset
Browse[1]> str(keep)
num [1:153899, 1:3415] -98.6 -95.8 -96.4 -95.8 -98 ...
Browse[1]> (density(keep))
Call:
density.default(x = keep)
Data: keep (525565085 obs.); Bandwidth 'bw' = 0.1695
x y
Min. :-106.94 Min. :0.0000000
1st Qu.: -87.13 1st Qu.:0.0003593
Median : -67.31 Median :0.0050385
Mean : -67.31 Mean :0.0126038
3rd Qu.: -47.49 3rd Qu.:0.0084189
Max. : -27.67 Max. :0.1792683
and as you can see is rather large... that makes it a marathon to print it.
Would it be possible to average some of the arrows in a column by column basis
so to turn the
str(keep)
num [1:153899, 1:3415] -98.6 -95.8 -96.4 -95.8 -98 ...
to something like
num [1:1000, 1:3415] . How can I do that efficiently in R?
I would like to thank everyone in advance.
Regards
Alex
________________________________
From: Dennis Murphy <djmuser@gmail.com>
Sent: Tuesday, March 26, 2013 10:43 AM
Subject: Re: [R] Plot Matrix with Data
Hi:
I don't understand why you're truncating the duty cycle at 90 when the
data range from 15 to 150 - the effect of doing that is the set of
grey squares in the heatmap corresponding to missing values.
The reason your color scale didn't work is because you never used it
in the ggplot() call, but I think that it would have been difficult to
incorporate it anyway because ggplot2 has its own conventions. It
appears that what you want is ggplot2::scale_fill_continuous().
I chucked the color factor and tried the following instead. You can
edit it however you want, and you can extend the length/width of the
color bar - see the help page of guide_colourbar(), in particular the
barheight argument.
http://docs.ggplot2.org/current/guide_colourbar.html
My shot at your graphic:
ggplot(tdm, aes(x = Var1, y = Var2, fill = value)) +
geom_raster() +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
scale_fill_continuous(limits = c(15, 150),
breaks = seq(15, 150, by = 15),
low = "orange", high = "red",
guide = guide_colourbar(nbin = 28)) +
labs(x = "MHz", y = "Threshold", fill = "Duty
Cycle")
This color bar has 28 bins, 5 units apart, but the labels are 15 apart
since otherwise they overwrite each other. You can reduce the crowding
by increasing the height of the color bar as indicated earlier. This
appears to be closer to your intent with a lot less bother.
Dennis
> 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 <djmuser@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]]