Hi, On Thu, Feb 21, 2013 at 9:21 AM, Alaios <ala...@yahoo.com> wrote: > Let me try to explain this then. > > I have numbers like > > DataToPlot > 4 5 6 > 1 0.4454995 0.4462009 0.4286807 > 2 0.3761550 0.5423205 0.6500785 > 3 0.3779496 0.4671437 0.1799601 > > and I want to have a color legend with each color that is mapped to the > following printed values > > "1","0.9","0.8","0.7","0.6","0.5","0.4","0.3","0.2","0.1","0"
Are these numbers or not? Is .1 < .2 < 3 etc.? If the answer is yes then I would use a continuous color scale. If not then I would use something besides numbers to represent the categories. > > For making life easier I have rounded up my number to one digit so OK, but it's still a number no? > > > matrixToPlot<-matrix(data=round(matrixToPlot,digits=1),nrow=nrow(matrixToPlot),ncol=ncol(matrixToPlot),dimnames=list(yLabel, > xLabel)) > matrixToPlot > 1 2 3 > 1 0.4 0.4 0.4 > 2 0.4 0.5 0.7 > 3 0.4 0.5 0.2 > > that indeed helps and makes the legend bar print the right values BUT there > are not all the ranges printed there. So for the given example data set only > the 0.2, 0.3,0.4,0.5,0.6,0.7 are printed with the 0,0.1,0.8,0.9,1 missing. Because your data does not have values of 0, .01, .08, .09, or 1! If value is a number than these non-existent values are meaningful, but if it is categorical they are not. So if want these values treat value as numeric. > Also I need some help to specify specific color to each of the given > categories/ranges in the colorbar Earlier you said you wanted grayscale. Is that still true? If so either treat value as continuous and use scale_fill_gradient() as I suggested earlier, or treat is as categorical and use scale_fill_grey() > my code now looks like > > > library(reshape2) > library(ggplot2) > > matrixToPlot<-matrix(data=runif(9),nrow=3,dimnames=list(seq(1,3),seq(4,6))) > > matrixToPlot<-matrix(data=round(matrixToPlot,digits=1),nrow=nrow(matrixToPlot),ncol=ncol(matrixToPlot),dimnames=list(seq(1:3), > seq(4:6))) > tdm <- melt(matrixToPlot) > cols<-colours() > cols<-cols[1:(nrow(matrixToPlot)*ncol(matrixToPlot))] > > p<- ggplot(tdm, aes(x = Var2, y = Var1, fill = factor(value))) + > labs(x = "MHz", y = "Threshold", fill = "Duty Cycle") + > geom_raster(alpha=.5) + > scale_fill_discrete(h.start=1) + > scale_x_continuous(expand = c(0, 0)) + > scale_y_continuous(expand = c(0, 0)) I still don't see what is wrong with my earlier suggestion. If you want labels at intervals of .1 you can use p<- ggplot(tdm, aes(x = Var2, y = Var1)) + labs(x = "MHz", y = "Threshold", fill = "Duty Cycle") + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) p + geom_raster(aes(fill=value), alpha=.5) + scale_fill_gradient(low="black", high="gray90", breaks=seq(0,1, .1)) If you don't like that solution please explain why! If you really want to specify the colors yourself you can go to all the trouble of dum.dat <- data.frame(Var1=1, Var2=1, value=factor(seq(0, 1, .1))) p + geom_point(aes(fill=value), size=0, data=dum.dat) + geom_raster(aes(fill=factor(value)), alpha=.5) + scale_fill_manual(values = paste("gray", seq(20, 100, by=8), sep="")) but a) now you are fighting with ggplot instead of taking advantage of it, and b) I fail to see how this is an improvement. Best, Ista > > > Regards > Alex > > > ________________________________ > From: Ista Zahn <istaz...@gmail.com> > To: Alaios <ala...@yahoo.com> > Cc: R help <R-help@r-project.org> > Sent: Thursday, February 21, 2013 2:15 PM > > Subject: Re: [R] ggplot2 customizing a plot > > Hi, > > On Thu, Feb 21, 2013 at 4:12 AM, Alaios <ala...@yahoo.com> wrote: >> Hello Ista, >> I would like to thank you for your reply! >> >> Your code is indeed an improvement. >> >> This is my code now and there are two things still missing. >> >> DataToPlot<-matrix(data=runif(9),nrow=3,dimnames=list(seq(1,3),seq(4,6))) >> require(reshape) >> require(ggplot2) >> require(raster) >> >> >> cols<-colours() >> cols<-cols[1:(nrow(DataToPlot)*ncol(DataToPlot))] >> tdm<-melt(DataToPlot) >> >> p<- ggplot(tdm, aes(x = Var2, y = Var1, fill = factor(value))) + >> labs(x = "MHz", y = "Threshold", fill = "Duty Cycle") + >> geom_raster(alpha=.5) + >> >> scale_fill_manual(values=cols,breaks=c("1","0.9","0.8","0.7","0.6","0.5","0.4","0.3","0.2","0.1","0")) > > value looks like numbers, why are you converting it to a factor? > > why this is how I found I should solve the problem...... > > >> >> My values go from 0 to 1 (might go slightly over the limits) and I want to >> specify one color for the following ranges, 1, 0.9,0.8,.....,0 . That >> means >> that values between the intervals will be "categorized" based on their >> closest match. > > I don't really understand this. > > > >> What I also tried is to create a gray scale pallete as this is printer >> friendly so the values from 0.9 to 0.1 get a greyish tone but I failed to >> do >> that with colours. > > How about > > ggplot(tdm, aes(x = Var2, y = Var1, fill = value)) + > labs(x = "MHz", y = "Threshold", fill = "Duty Cycle") + > geom_raster(alpha=.5) + > scale_fill_gradient(low="gray90", high="black") + > theme_bw() > >> >> Could you please help me have both >> a. A fixed scale where values are categorized there >> and > > Value is numeric, and trying to treat it like a category is only going > to make things difficult. > >> b. a greyish pallete for the categories? > > Use scale_fill_gradient with different shades of gray as the low and > high values, as shown above. > > Best, > Ista > >> >> I would like to thank you in advance for your reply >> >> Regards >> Alex >> >> >> ________________________________ >> From: Ista Zahn <istaz...@gmail.com> >> To: Alaios <ala...@yahoo.com> >> Cc: R help <R-help@r-project.org> >> Sent: Wednesday, February 20, 2013 4:54 PM >> Subject: Re: [R] ggplot2 customizing a plot >> >> Hi, >> >> On Wed, Feb 20, 2013 at 9:33 AM, Alaios <ala...@yahoo.com> wrote: >>> Dear all, >>> I want some help improve my ggplot as following: >>> Make the plottable area with grid, so is easy one to see where each box >>> refers to x and y values. >> >> I don't think you can easily move the grid to the front, but you can >> make the tiles transparent so the grid can be seen through them by >> setting the alpha < 1. >> >>> Add a color bar but with fixed values, that I want to specify. >> >> use scale_fill_manual >> >>> How I can do those two? >> >> p<- ggplot(tdm, aes(x = Var2, y = Var1, fill = factor(value))) + >> labs(x = "MHz", y = "Threshold", fill = "Duty Cycle") + >> geom_raster(alpha=.5) + >> scale_fill_manual(values=c("red", "blue", "green", "purple", >> "orange", "pink", "tan", "violet", "yellow")) >> >> >> Best, >> Ista >> >> >>> >>> Before is some code what I have tried so far. >>> >>> Regards >>> Alex >>> >>> >>> DataToPlot<-matrix(data=seq(1:9),nrow=3,dimnames=list(seq(1,3),seq(4,6))) >>> >>> >>> require(reshape) >>> require(ggplot2) >>> require(raster) >>> >>> tdm <- melt(DataToPlot) >>> >>> >>> >>> p<- ggplot(tdm, aes(x = Var2, y = Var1, fill = factor(value))) + >>> labs(x = "MHz", y = "Threshold", fill = "Duty Cycle") + >>> geom_raster() + >>> scale_fill_discrete() >>> >>> [[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. >>> >> >> > > ______________________________________________ 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.