ok, I think I got it. Here is the code I used. Thanks for the help guys..
fake <- cbind(c(1,2,5,8,12,19), c(2,5,8,12,19,20), c(1,1,1,1,1,1), c(-10,-10,-10,-10,-10,-10), c(10,10,10,10,10,10), runif(6, 0, 2), runif(6, 0, 2)) datalim <- c(-0,10) matrix3plus <- fake[,-(1:2)] dimnames(fake)[[2]] <- letters[1:dim(fake)[2]] rbPal <- colorRampPalette(c('blue', 'white','red')) gradcol <- rbPal(100) mycolors <- cbind(fake[,1:2], array(rbPal(100)[as.numeric(cut(matrix3plus, breaks=100))], dim=dim(matrix3plus))) # Plot the color key on the lower part ColorBar(lut=gradcol, min=datalim[1], max=datalim[2]) par(new=TRUE) plot(c(fake[1,1],fake[length(fake[,1]),2]), c(0,1), col="White", xlab="", ylab="", main=title, axes=F) yheight <- 1/length(ycenter) ycenter <- seq(from=0, to=1, length=(length[fake[1,]])) trackheight <- ycenter[2] for(i in 3:length(fake[1,])){ RectPlotter(matrixval=fake[,c(1,2,i)], ycoord=ycenter[length(ycenter)-i+2-emptylines], trackheight=ycenter[2], mycolors=mycolors[,i]) text(fake[1,1], ycenter[length(ycenter)-i+2-emptylines], colnames(fake)[i], cex=cexsize, pos=4) } On Jan 10, 2014, at 15:47, Pachapep <pacha...@gmail.com> wrote: > Sorry for bugging you again. I was wondering whether it is possible to > include multiple different colors instead of just two. > In the example below, the color.scale() function goes from yellow to red, but > never passes through plain white. Would it be possible to tweak the function > or would I have to start all over again? > Thanks for any tips. > patrick > > On Jan 9, 2014, at 7:54, Pachapep <pacha...@gmail.com> wrote: > >> >> Great, it works perfectly! Thanks so much for the awesome help! >> (Happy) patrick >> >> On Jan 9, 2014, at 7:07, Adams, Jean <jvad...@usgs.gov> wrote: >> >>> Patrick, >>> >>> No error in your code, just two different ways of deriving a range of >>> colors ... heat.colors() and color.scale(). I modified the code to just >>> use color.scale(). You can tell it what two colors you want it to use for >>> the extremes, and it will work out the shades in between. >>> >>> Jean >>> >>> >>> >>> # fake data >>> fake <- cbind(c(1,2,5,8,12,19), c(2,5,8,12,19,20), c(1,1,1,1,1,1), >>> runif(6, 0, 2), runif(6, 0, 2)) >>> >>> mymatrix <- fake >>> dimnames(mymatrix)[[2]] <- letters[1:dim(fake)[2]] >>> >>> # separate the first two columns from the remaining columns for simplicity >>> mymatrix12 <- mymatrix[, 1:2] >>> mymatrix3plus <- mymatrix[, -(1:2)] >>> # create variables for the dimensions for easy reference later >>> nrows <- dim(mymatrix3plus)[1] >>> ncols <- dim(mymatrix3plus)[2] >>> >>> # to make sure that the min and max contain all of the data >>> # use floor(100*...)/100 and ceiling(100*...)/100 instead of round(..., 2) >>> datalim <- c(floor(100*min(mymatrix3plus)), >>> ceiling(100*max(mymatrix3plus)))/100 >>> # choose two colors to be the end points >>> colorlim <- c("yellow", "red") >>> # create a sequence of 10 colors across the selected range for the legend >>> lut <- color.scale(seq(datalim[1], datalim[2], length=10), >>> extremes=colorlim, xrange=datalim) >>> # it's not clear from the help file, but the extremes= argument wants a >>> range (a vector of length 2) >>> mycolors <- array(color.scale(mymatrix3plus, extremes=colorlim, >>> xrange=datalim), dim=dim(mymatrix3plus)) >>> ycenter <- seq(from=0, to=1, length=(nrows + 2)) >>> yheight <- 1/(nrows + 2) >>> >>> # Plot the color key on the lower part >>> ColorBar(lut=lut, min=datalim[1], max=datalim[2]) >>> >>> # Plot the actual heatmap >>> par(new=TRUE) >>> # can't use title for main= argument, because title only exists inside the >>> ColorBar() function >>> plot(c(mymatrix12[1, 1], mymatrix12[nrows, 2]), c(0, 1), col="White", >>> xlab="", ylab="", main="SOMETHING", axes=F) >>> for(i in 1:ncols){ >>> RectPlotter(mymatrix12, ycenter[nrows - i + 2], yheight, mycolors[, i]) >>> text(mymatrix12[1,1], ycenter[nrows - i + 2], >>> colnames(mymatrix3plus)[i], pos=4) >>> } >>> >>> >>> >>> On Wed, Jan 8, 2014 at 3:59 PM, Pachapep <pacha...@gmail.com> wrote: >>> >>> Hi Jean, >>> Thanks for the great help. Indeed, it seems that that helps a bit. However, >>> I included a control column made of ones. >>> >>>> fake <- cbind(c(1,2,5,8,12,19), c(2,5,8,12,19,20), c(1,1,1,1,1,1), >>>> runif(6, 0, 2), runif(6, 0, 2), runif(6, 0, 2)) >>> >>> However, the color doesnt correspond to that in the key (it is red-ish in >>> the plot while the 1 corresponds to yellow-ish in the key), so Im not sure >>> whether it is the key or the colors that are messed up. Sorry for all these >>> questions.. and thanks (again) for the help provided. >>> >>> >>> >>> >>> >>> >>> >>> On Jan 8, 2014, at 16:15, Adams, Jean <jvad...@usgs.gov> wrote: >>> >>>> Patrick, >>>> >>>> Thanks for providing reproducible code! >>>> >>>> I think the main problem was that the extremes= argument in the >>>> color.scale() function wants a range (a vector of length 2), and you were >>>> providing with more than that, length(lut) is 10. >>>> >>>> In the process of tracking this down, I made a bunch of minor changes to >>>> your code to help me see what was going on. This is what I ended up with. >>>> >>>> Jean >>>> >>>> >>>> >>>> # fake data >>>> fake <- cbind(c(1,2,5,8,12,19), c(2,5,8,12,19,20), c(1,1,1,1,1,1), >>>> runif(6, 0, 2), runif(6, 0, 2), runif(6, 0, 2)) >>>> mymatrix <- fake >>>> dimnames(mymatrix)[[2]] <- letters[1:5] >>>> >>>> # separate the first two columns from the remaining columns for simplicity >>>> mymatrix12 <- mymatrix[, 1:2] >>>> mymatrix3plus <- mymatrix[, -(1:2)] >>>> # create variables for the dimensions for easy reference later >>>> nrows <- dim(mymatrix3plus)[1] >>>> ncols <- dim(mymatrix3plus)[2] >>>> >>>> # prepare plotting parameters >>>> lut <- rev(heat.colors(10)) >>>> # to make sure that the min and max contain all of the data >>>> # use floor(100*...)/100 and ceiling(100*...)/100 instead of round(..., 2) >>>> minimum <- floor(100*min(mymatrix3plus))/100 >>>> maximum <- ceiling(100*max(mymatrix3plus))/100 >>>> # it's not clear from the help file, but the extremes= argument wants a >>>> range (a vector of length 2) >>>> mycolors <- color.scale(mymatrix3plus, extremes=lut[c(1, length(lut))]) >>>> ycenter <- seq(from=0, to=1, length=(nrows + 2)) >>>> yheight <- 1/(nrows + 2) >>>> >>>> # Plot the color key on the lower part >>>> ColorBar(lut=lut, min=minimum, max=maximum) >>>> >>>> # Plot the actual heatmap >>>> par(new=TRUE) >>>> # can't use title for main= argument, because title only exists inside the >>>> ColorBar() function >>>> plot(c(mymatrix12[1, 1], mymatrix12[nrows, 2]), c(0, 1), col="White", >>>> xlab="", ylab="", main="SOMETHING", axes=F) >>>> for(i in 1:ncols){ >>>> RectPlotter(mymatrix12, ycenter[nrows - i + 2], yheight, mycolors[, i]) >>>> text(mymatrix12[1,1], ycenter[nrows - i + 2], >>>> colnames(mymatrix3plus)[i], pos=4) >>>> } >>>> >>>> >>>> >>>> On Wed, Jan 8, 2014 at 2:13 PM, Pachapep <pacha...@gmail.com> wrote: >>>> >>>> Hi Jean, >>>> Thanks a ton for the help. I think Im almost there, but there is still >>>> something weird about my stuff. >>>> I have been able to understand the color.scale() function. Now, I am >>>> trying to plot a key for the corresponding colors. The function is called >>>> ColorBar, which apparently works - the colors are indeed lut <- >>>> rev(heat.colors(10)) and go from my minimum to my maximum values. However, >>>> when I try to apply this to my mycolors( and then plot it, I suddenly have >>>> colors that seem to be outside of the min-max (in the greens and blacks), >>>> instead of the white to red gradient. Hope this is not too complicated of >>>> a code, but I think copy-paste it should give the (problematic) result. I >>>> guess there is a small error somewhere, but I cant figure out what I have >>>> done wrong :-( >>>> Thanks a lot for any suggestion. >>>> Patrick >>>> >>>> >>>> #------------------------------------------------------------ >>>> library(plotrix) >>>> #------------------------------------------------------------ >>>> # ColorBar >>>> ColorBar <- function(lut, min, max, nticks=5, ticks=seq(min, max, >>>> len=nticks), title='') { >>>> scale = (length(lut)-1)/(max-min) >>>> plot(c(min,max),c(0,10), type='n', bty='n', xaxt='n', xlab='', yaxt='n', >>>> ylab='', main=title) >>>> axis(1, ticks, las=1, cex.axis=0.5, lwd=0.5) >>>> for (i in 1:(length(lut)-1)) { >>>> y = (i-1)/scale + min >>>> rect(y,0,y+1/scale,0.5, col=lut[i], border=NA) >>>> } >>>> } >>>> >>>> #------------------------------------------------------------ >>>> # RectPlotter >>>> RectPlotter <- function(matrixval, ycoord, height, mycolors){ >>>> rect(matrixval[,1], (ycoord-(0.4*height)), matrixval[,2], >>>> (ycoord+(0.4*height)), col=mycolors, border = NA) >>>> } >>>> >>>> fake <- cbind(c(1,2,5,8,12,19), c(2,5,8,12,19,20), runif(6, 0, 2), >>>> runif(6, 0, 2), runif(6, 0, 2)) >>>> mymatrix <- fake >>>> # Plot the color key on the lower part >>>> lut <- rev(heat.colors(10)) >>>> minimum <- round(min(mymatrix[,-(1:2)]), digits=2) >>>> maximum <- round(max(mymatrix[,-(1:2)]), digits=2) >>>> ColorBar(lut=lut, min=minimum, max=maximum) >>>> par(new=TRUE) >>>> #____________________________________________ >>>> #_______________PROBLEM HERE_______________ >>>> mycolors <- cbind(mymatrix[,1:2],color.scale(mymatrix[,-(1:2)], >>>> extremes=lut)) >>>> plot(c(mymatrix[1,1],mymatrix[length(mymatrix[,1]),2]), c(0,1), >>>> col="White", xlab="", ylab="", main=title, axes=F) >>>> ycenter <- seq(from=0, to=1, length=(length(mymatrix[1,])+2)) >>>> yheight <- 1/length(ycenter) >>>> >>>> # Plot the actual heatmap >>>> for(i in 3:length(mymatrix[1,])){ >>>> #for(i in 3:5){ >>>> RectPlotter(mymatrix[,c(1,2,i)], ycenter[length(ycenter)-i+2], >>>> yheight, mycolors[,i]) >>>> text(mymatrix[1,1], ycenter[length(ycenter)-i+2], >>>> colnames(mymatrix)[i], cex=0.5, pos=4) >>>> } >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> On Jan 7, 2014, at 18:48, Adams, Jean <jvad...@usgs.gov> wrote: >>>> >>>>> Patrick, >>>>> >>>>> You should cc r-help on all correspondence so that others can follow the >>>>> thread. >>>>> >>>>> The color range will be matched to the "x" argument you provide to the >>>>> color.scale function (in package plotrix). So you don't need to manually >>>>> provide the min and max yourself. If for some reason you did need to >>>>> provide that information, you would use the xrange argument not the >>>>> extremes argument (which is expecting colors). >>>>> >>>>> mycolors <- color.scale(mymatrix[,-(1:2)], c(0,1,1), c(1,1,0), c(1,0,1)) >>>>> >>>>> Jean >>>>> >>>>> >>>>> On Tue, Jan 7, 2014 at 4:16 PM, Pachapep <pacha...@gmail.com> wrote: >>>>> >>>>> Hi Jean >>>>> Thanks for the quick answer. It looks like it works, but the issue is >>>>> that my real data fluctuates between 0 and 2, so I everything is red. >>>>> I tried using another function (color.scale), which works but gives me >>>>> grey scale only (no colors) although I dont really understand why - its >>>>> a detail, but it's kind of a bummer.. >>>>> >>>>> mycolors <- color.scale(mymatrix[,-(1:2)], c(0,1,1), c(1,1,0), c(1,0,1), >>>>> extremes=c(min(mymatrix[,-(1:2)]), max(mymatrix[,-(1:2)]))) >>>>> >>>>> Is there a way to make an array using the min and max of the matrix (as I >>>>> used in color.scale() above) so that the colors go from blue to red? >>>>> Thanks again for the help. >>>>> Patrick >>>>> >>>>> On Jan 7, 2014, at 15:04, Adams, Jean <jvad...@usgs.gov> wrote: >>>>> >>>>>> Patrick, >>>>>> >>>>>> You were pretty close. >>>>>> To fix the code you have, just change "matrix" to "mymatrix" in two >>>>>> places, and either specify the argument data= or place the heat.colors >>>>>> bit first in the matrix function. >>>>>> >>>>>> Or ... you could use the array() function instead, to shorten up the >>>>>> code a little. >>>>>> mycol <- array(heat.colors(max(mymatrix[, 3:5]))[mymatrix[, 3:5]], >>>>>> dim=dim(mymatrix)) >>>>>> >>>>>> Jean >>>>>> >>>>>> >>>>>> On Mon, Jan 6, 2014 at 9:19 PM, Pachapep <pacha...@gmail.com> wrote: >>>>>> Hi all, >>>>>> I have a matrix which I want to plot in color. I have extensively looked >>>>>> at level plot and heatmap/heatmap.2, but I would like to be able to >>>>>> manage the size of the bins (boxes) on my X axis. So I thought of >>>>>> simply using the rect() function, but I cant get around assigning the >>>>>> correct colors to all the values. I have built a RectPlotter() function >>>>>> that takes the start and stop as well as the value assigned to each >>>>>> bin. >>>>>> Here is what I have done so far (after countless hours of despair): >>>>>> >>>>>> RectPlotter <- function(matrixval, ycoord, height, mycolors){ >>>>>> rect(matrixval[,1], (ycoord-(0.4*height)), matrixval[,2], >>>>>> (ycoord+(0.4*height)), col=mycolors, border = NA) >>>>>> } >>>>>> >>>>>> starts <- c(1,5,8,15) >>>>>> ends <- c(5, 8, 15, 25) >>>>>> vals1 <- c(2, 15, 7, 13) >>>>>> vals2 <- c(7, 2, 1, 26) >>>>>> vals3 <- c(52, 1, 29, 18) >>>>>> mymatrix <- cbind(starts, ends, vals1, vals2, vals3) >>>>>> # This gives me an error >>>>>> mycol<- matrix(nrow=dim(mymatrix)[1], ncol=dim(mymatrix)[2], >>>>>> heat.colors(max(matrix[,3:5]))[matrix[,3:5]]) >>>>>> >>>>>> >>>>>> At the end, I would like to have something like a levelplot, but using >>>>>> the starts and ends as x-axis. >>>>>> Any help would be highly appreciated. Thanks! >>>>>> Patrick >>>>>> ______________________________________________ >>>>>> 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.