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), 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 I’m 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 can’t 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 don’t really understand why - it’s 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 can’t 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<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