divergent colors around zero in levelplot()

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

divergent colors around zero in levelplot()

Don McKenzie-2
I would like to produce a levelplot with divergent colors such that increasingly negative values of Z get darker in the first color and increasingly
positive values get darker in the second color.  this is common in cartography. I have tried tinkering with the col.regions argument but the best I can do
is to get the split in the middle of my range of Z, but in my particular case range(Z) is (-1,12).

I am using R 3.0.2 on OSX 10.9

Here is an example

x <- y <- c(1:25)
grid <- expand.grid(x=x,y=y)
grid$z <- sort(runif(625,min=-1,max=12))
levelplot(z ~ x*y,grid)   # produces the default pink and blue but the split is at ~5.5

# do something clever here
# e.g., my.colors <- <create a palette that splits at zero>

levelplot(z ~ x*y,grid,col.regions=my.colors)  # so there should be some light pink at the bottom and the rest increasingly intense blue

Ideas appreciated.  Thanks in advance.



Don McKenzie
Research Ecologist
Pacific Wildland Fire Sciences Lab
US Forest Service

Affiliate Professor
School of Environmental and Forest Sciences
University of Washington
 
[hidden email]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: divergent colors around zero in levelplot()

Bert Gunter
Use the Rcolorbrewer package.

-- Bert

On Fri, Nov 22, 2013 at 8:43 PM, Don McKenzie <[hidden email]> wrote:

> I would like to produce a levelplot with divergent colors such that increasingly negative values of Z get darker in the first color and increasingly
> positive values get darker in the second color.  this is common in cartography. I have tried tinkering with the col.regions argument but the best I can do
> is to get the split in the middle of my range of Z, but in my particular case range(Z) is (-1,12).
>
> I am using R 3.0.2 on OSX 10.9
>
> Here is an example
>
> x <- y <- c(1:25)
> grid <- expand.grid(x=x,y=y)
> grid$z <- sort(runif(625,min=-1,max=12))
> levelplot(z ~ x*y,grid)   # produces the default pink and blue but the split is at ~5.5
>
> # do something clever here
> # e.g., my.colors <- <create a palette that splits at zero>
>
> levelplot(z ~ x*y,grid,col.regions=my.colors)  # so there should be some light pink at the bottom and the rest increasingly intense blue
>
> Ideas appreciated.  Thanks in advance.
>
>
>
> Don McKenzie
> Research Ecologist
> Pacific Wildland Fire Sciences Lab
> US Forest Service
>
> Affiliate Professor
> School of Environmental and Forest Sciences
> University of Washington
>
> [hidden email]
>
> ______________________________________________
> [hidden email] 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.



--

Bert Gunter
Genentech Nonclinical Biostatistics

(650) 467-7374

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: divergent colors around zero in levelplot()

Don McKenzie-2
Thanks Bert.  I’ll check it out.

Don

On Nov 22, 2013, at 10:25 PM, Bert Gunter <[hidden email]> wrote:

> Use the Rcolorbrewer package.
>
> -- Bert
>
> On Fri, Nov 22, 2013 at 8:43 PM, Don McKenzie <[hidden email]> wrote:
>> I would like to produce a levelplot with divergent colors such that increasingly negative values of Z get darker in the first color and increasingly
>> positive values get darker in the second color.  this is common in cartography. I have tried tinkering with the col.regions argument but the best I can do
>> is to get the split in the middle of my range of Z, but in my particular case range(Z) is (-1,12).
>>
>> I am using R 3.0.2 on OSX 10.9
>>
>> Here is an example
>>
>> x <- y <- c(1:25)
>> grid <- expand.grid(x=x,y=y)
>> grid$z <- sort(runif(625,min=-1,max=12))
>> levelplot(z ~ x*y,grid)   # produces the default pink and blue but the split is at ~5.5
>>
>> # do something clever here
>> # e.g., my.colors <- <create a palette that splits at zero>
>>
>> levelplot(z ~ x*y,grid,col.regions=my.colors)  # so there should be some light pink at the bottom and the rest increasingly intense blue
>>
>> Ideas appreciated.  Thanks in advance.
>>
>>
>>
>> Don McKenzie
>> Research Ecologist
>> Pacific Wildland Fire Sciences Lab
>> US Forest Service
>>
>> Affiliate Professor
>> School of Environmental and Forest Sciences
>> University of Washington
>>
>> [hidden email]
>>
>> ______________________________________________
>> [hidden email] 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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> (650) 467-7374

Don McKenzie
Research Ecologist
Pacific WIldland Fire Sciences Lab
US Forest Service

Affiliate Professor
School of Environmental and Forest Sciences
College of the Environment
University of Washington
[hidden email]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: divergent colors around zero in levelplot()

Achim Zeileis-4
In reply to this post by Don McKenzie-2
On Fri, 22 Nov 2013, Don McKenzie wrote:

> I would like to produce a levelplot with divergent colors such that increasingly negative values of Z get darker in the first color and increasingly
> positive values get darker in the second color.  this is common in cartography. I have tried tinkering with the col.regions argument but the best I can do
> is to get the split in the middle of my range of Z, but in my particular case range(Z) is (-1,12).
>
> I am using R 3.0.2 on OSX 10.9
>
> Here is an example
>
> x <- y <- c(1:25)
> grid <- expand.grid(x=x,y=y)
> grid$z <- sort(runif(625,min=-1,max=12))
> levelplot(z ~ x*y,grid)   # produces the default pink and blue but the split is at ~5.5
>
> # do something clever here
> # e.g., my.colors <- <create a palette that splits at zero>
>
> levelplot(z ~ x*y,grid,col.regions=my.colors)  # so there should be some light pink at the bottom and the rest increasingly intense blue
>
> Ideas appreciated.  Thanks in advance.

One approach is to limit the range of colors (to match the range of the
data) as you suggest above. The other approach is to extend the range of
the legend (beyond the range of the data). For example:

levelplot(z ~ x*y, grid, at = seq(-12, 12, length = 100))

This produces a legend that is symmetric around zero.

For other/better diverging color palettes, you can use the RColorBrewer
package (as suggested by Bert) or the colorspace package (see e.g., its
graphical choose_color() tool).

>
>
> Don McKenzie
> Research Ecologist
> Pacific Wildland Fire Sciences Lab
> US Forest Service
>
> Affiliate Professor
> School of Environmental and Forest Sciences
> University of Washington
>
> [hidden email]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

specify breaks in divergent palette in RColorBrewer: was divergent colors around zero in levelplot()

Don McKenzie-2
In reply to this post by Bert Gunter
Bert or anyone else familiar with RColorBrewer:

Has anyone tried to accomplish with RColorBrewer what I asked about in my original post (below)?

Here is an example cribbed from the levelplot() help examples

x <- seq(pi/4, 5 * pi, length.out = 100)
y <- seq(pi/4, 5 * pi, length.out = 100)
r <- as.vector(sqrt(outer(x^2, y^2, "+")))
grid <- expand.grid(x=x, y=y)
grid$z <- cos(r^2) * exp(-r/(pi^3))

# now use RColorBrewer to get a palette

library("RColorBrewer”)
levelplot(z~x*y, grid,col.regions=brewer.pal(6,"BrBG”))   # the numeric argument to brewer.pal is the number of colors used — I tried several

This gives me a nice brown-to-green gradient but does not (AFAICS) give me control over where the center of the divergence lies. Even in this symmetrical
example, I can’t get it to be at zero — it repeats on either side of zero.

thanks to anyone who pages through all this and makes a suggestion, even if it doesn’t work.  :-)

On Nov 22, 2013, at 10:25 PM, Bert Gunter <[hidden email]> wrote:

> Use the Rcolorbrewer package.
>
> -- Bert
>
> On Fri, Nov 22, 2013 at 8:43 PM, Don McKenzie <[hidden email]> wrote:
>> I would like to produce a levelplot with divergent colors such that increasingly negative values of Z get darker in the first color and increasingly
>> positive values get darker in the second color.  this is common in cartography. I have tried tinkering with the col.regions argument but the best I can do
>> is to get the split in the middle of my range of Z, but in my particular case range(Z) is (-1,12).
>>
>> I am using R 3.0.2 on OSX 10.9
>>
>> Here is an example
>>
>> x <- y <- c(1:25)
>> grid <- expand.grid(x=x,y=y)
>> grid$z <- sort(runif(625,min=-1,max=12))
>> levelplot(z ~ x*y,grid)   # produces the default pink and blue but the split is at ~5.5
>>
>> # do something clever here
>> # e.g., my.colors <- <create a palette that splits at zero>
>>
>> levelplot(z ~ x*y,grid,col.regions=my.colors)  # so there should be some light pink at the bottom and the rest increasingly intense blue
>>
>> Ideas appreciated.  Thanks in advance.
>>
>>
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> (650) 467-7374

Don McKenzie
Research Ecologist
Pacific Wildland Fire Science Lab
US Forest Service

Affiliate Professor
School of Environmental and Forest Sciences
University of Washington
[hidden email]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: specify breaks in divergent palette in RColorBrewer: was divergent colors around zero in levelplot()

Don McKenzie-2
Never mind.   Solved.  “cuts” argument back in levelplot(). Duh.

On Nov 25, 2013, at 4:27 PM, Don McKenzie <[hidden email]> wrote:

> Bert or anyone else familiar with RColorBrewer:
>
> Has anyone tried to accomplish with RColorBrewer what I asked about in my original post (below)?
>
> Here is an example cribbed from the levelplot() help examples
>
> x <- seq(pi/4, 5 * pi, length.out = 100)
> y <- seq(pi/4, 5 * pi, length.out = 100)
> r <- as.vector(sqrt(outer(x^2, y^2, "+")))
> grid <- expand.grid(x=x, y=y)
> grid$z <- cos(r^2) * exp(-r/(pi^3))
>
> # now use RColorBrewer to get a palette
>
> library("RColorBrewer”)
> levelplot(z~x*y, grid,col.regions=brewer.pal(6,"BrBG”))   # the numeric argument to brewer.pal is the number of colors used — I tried several
>
> This gives me a nice brown-to-green gradient but does not (AFAICS) give me control over where the center of the divergence lies. Even in this symmetrical
> example, I can’t get it to be at zero — it repeats on either side of zero.
>
> thanks to anyone who pages through all this and makes a suggestion, even if it doesn’t work.  :-)
>
> On Nov 22, 2013, at 10:25 PM, Bert Gunter <[hidden email]> wrote:
>
>> Use the Rcolorbrewer package.
>>
>> -- Bert
>>
>> On Fri, Nov 22, 2013 at 8:43 PM, Don McKenzie <[hidden email]> wrote:
>>> I would like to produce a levelplot with divergent colors such that increasingly negative values of Z get darker in the first color and increasingly
>>> positive values get darker in the second color.  this is common in cartography. I have tried tinkering with the col.regions argument but the best I can do
>>> is to get the split in the middle of my range of Z, but in my particular case range(Z) is (-1,12).
>>>
>>> I am using R 3.0.2 on OSX 10.9
>>>
>>> Here is an example
>>>
>>> x <- y <- c(1:25)
>>> grid <- expand.grid(x=x,y=y)
>>> grid$z <- sort(runif(625,min=-1,max=12))
>>> levelplot(z ~ x*y,grid)   # produces the default pink and blue but the split is at ~5.5
>>>
>>> # do something clever here
>>> # e.g., my.colors <- <create a palette that splits at zero>
>>>
>>> levelplot(z ~ x*y,grid,col.regions=my.colors)  # so there should be some light pink at the bottom and the rest increasingly intense blue
>>>
>>> Ideas appreciated.  Thanks in advance.
>>>
>>>
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>>
>> (650) 467-7374
>
> Don McKenzie
> Research Ecologist
> Pacific Wildland Fire Science Lab
> US Forest Service
>
> Affiliate Professor
> School of Environmental and Forest Sciences
> University of Washington
> [hidden email]
>
>
>

Don McKenzie
Research Ecologist
Pacific Wildland Fire Science Lab
US Forest Service

Affiliate Professor
School of Environmental and Forest Sciences
University of Washington
[hidden email]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: specify breaks in divergent palette in RColorBrewer: was divergent colors around zero in levelplot()

C. Alina Cansler
In reply to this post by Don McKenzie-2
Don,

This looks helpful:
https://stat.ethz.ch/pipermail/r-help/2011-March/272361.html

Also, here is some code that I had, and tried to make applicable to your
question:

div.colors <-colorRampPalette(c("blue", "white", "red" ))
x<-seq(-1,12,1);x
palette(div.colors(length(x)))
y<- rep(1,length(x))
barplot(y,  col=x+2, space=0, axes=FALSE, border = NA  ,   cex.names=1.3 ,
xlab="", ylab="",xaxt="n")

-Alina




On Mon, Nov 25, 2013 at 4:27 PM, Don McKenzie <[hidden email]> wrote:

> Bert or anyone else familiar with RColorBrewer:
>
> Has anyone tried to accomplish with RColorBrewer what I asked about in my
> original post (below)?
>
> Here is an example cribbed from the levelplot() help examples
>
> x <- seq(pi/4, 5 * pi, length.out = 100)
> y <- seq(pi/4, 5 * pi, length.out = 100)
> r <- as.vector(sqrt(outer(x^2, y^2, "+")))
> grid <- expand.grid(x=x, y=y)
> grid$z <- cos(r^2) * exp(-r/(pi^3))
>
> # now use RColorBrewer to get a palette
>
> library("RColorBrewer”)
> levelplot(z~x*y, grid,col.regions=brewer.pal(6,"BrBG”))   # the numeric
> argument to brewer.pal is the number of colors used — I tried several
>
> This gives me a nice brown-to-green gradient but does not (AFAICS) give me
> control over where the center of the divergence lies. Even in this
> symmetrical
> example, I can’t get it to be at zero — it repeats on either side of zero.
>
> thanks to anyone who pages through all this and makes a suggestion, even
> if it doesn’t work.  :-)
>
> On Nov 22, 2013, at 10:25 PM, Bert Gunter <[hidden email]> wrote:
>
> > Use the Rcolorbrewer package.
> >
> > -- Bert
> >
> > On Fri, Nov 22, 2013 at 8:43 PM, Don McKenzie <[hidden email]>
> wrote:
> >> I would like to produce a levelplot with divergent colors such that
> increasingly negative values of Z get darker in the first color and
> increasingly
> >> positive values get darker in the second color.  this is common in
> cartography. I have tried tinkering with the col.regions argument but the
> best I can do
> >> is to get the split in the middle of my range of Z, but in my
> particular case range(Z) is (-1,12).
> >>
> >> I am using R 3.0.2 on OSX 10.9
> >>
> >> Here is an example
> >>
> >> x <- y <- c(1:25)
> >> grid <- expand.grid(x=x,y=y)
> >> grid$z <- sort(runif(625,min=-1,max=12))
> >> levelplot(z ~ x*y,grid)   # produces the default pink and blue but the
> split is at ~5.5
> >>
> >> # do something clever here
> >> # e.g., my.colors <- <create a palette that splits at zero>
> >>
> >> levelplot(z ~ x*y,grid,col.regions=my.colors)  # so there should be
> some light pink at the bottom and the rest increasingly intense blue
> >>
> >> Ideas appreciated.  Thanks in advance.
> >>
> >>
> >
> > Bert Gunter
> > Genentech Nonclinical Biostatistics
> >
> > (650) 467-7374
>
> Don McKenzie
> Research Ecologist
> Pacific Wildland Fire Science Lab
> US Forest Service
>
> Affiliate Professor
> School of Environmental and Forest Sciences
> University of Washington
> [hidden email]
>
>
>
>

--
C. Alina Cansler
Fire and Mountain Ecology Lab
School of Environmental and Forest Sciences
College of the Environment
University of Washington
E-mail: [hidden email]
Cell: 206-794-1630

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: specify breaks in divergent palette in RColorBrewer: was divergent colors around zero in levelplot()

Don McKenzie-2
In reply to this post by Don McKenzie-2
In case anyone cares (?), here is a function to do what I was asking, which doesn’t use colorBrewer but could with some hacking. I’m sure it’s fragile, but it works with well behaved integers and zero in the middle, which was all I needed. The output is a palette that can be passed to levelplot() and other functions.

Cheers

diverge.color <- function(start.color,end.color,min.value,max.value,mid.value=0,mid.color="ivory")

{
        # based on ideas from Maureen Kennedy, Nick Povak, and Alina Cansler

        # creates a palette for the current session for a divergent-color
        # graphic with a non-symmetric range
        # "cuts" = the number of slices to be made in the range above and below "mid.value"

        ramp1 <- colorRampPalette(c(start.color,mid.color))
        ramp2 <- colorRampPalette(c(mid.color,end.color))

       # now specify the number of values on either side of "mid.value"
       
       max.breaks <- round(max.value - mid.value)
       min.breaks <- round(mid.value - min.value)
       
       num.breaks <- max(max.breaks,min.breaks)
       
       low.ramp <- ramp1(num.breaks)
       high.ramp <- ramp2(num.breaks)
       
       # now create a combined ramp from the higher values of "low.ramp" and
       # the lower values of "high.ramp", with the longer one using all values
       # high.ramp starts at 2 to avoid duplicating zero
       
       myColors <- c(low.ramp[(num.breaks-min.breaks):num.breaks],high.ramp[2:max.breaks])
 
      myColors
}
 

On Nov 25, 2013, at 4:27 PM, Don McKenzie <[hidden email]> wrote:

> Bert or anyone else familiar with RColorBrewer:
>
> Has anyone tried to accomplish with RColorBrewer what I asked about in my original post (below)?
>
> Here is an example cribbed from the levelplot() help examples
>
> x <- seq(pi/4, 5 * pi, length.out = 100)
> y <- seq(pi/4, 5 * pi, length.out = 100)
> r <- as.vector(sqrt(outer(x^2, y^2, "+")))
> grid <- expand.grid(x=x, y=y)
> grid$z <- cos(r^2) * exp(-r/(pi^3))
>
> # now use RColorBrewer to get a palette
>
> library("RColorBrewer”)
> levelplot(z~x*y, grid,col.regions=brewer.pal(6,"BrBG”))   # the numeric argument to brewer.pal is the number of colors used — I tried several
>
> This gives me a nice brown-to-green gradient but does not (AFAICS) give me control over where the center of the divergence lies. Even in this symmetrical
> example, I can’t get it to be at zero — it repeats on either side of zero.
>
> thanks to anyone who pages through all this and makes a suggestion, even if it doesn’t work.  :-)
>
> On Nov 22, 2013, at 10:25 PM, Bert Gunter <[hidden email]> wrote:
>
>> Use the Rcolorbrewer package.
>>
>> -- Bert
>>
>> On Fri, Nov 22, 2013 at 8:43 PM, Don McKenzie <[hidden email]> wrote:
>>> I would like to produce a levelplot with divergent colors such that increasingly negative values of Z get darker in the first color and increasingly
>>> positive values get darker in the second color.  this is common in cartography. I have tried tinkering with the col.regions argument but the best I can do
>>> is to get the split in the middle of my range of Z, but in my particular case range(Z) is (-1,12).
>>>
>>> I am using R 3.0.2 on OSX 10.9
>>>
>>> Here is an example
>>>
>>> x <- y <- c(1:25)
>>> grid <- expand.grid(x=x,y=y)
>>> grid$z <- sort(runif(625,min=-1,max=12))
>>> levelplot(z ~ x*y,grid)   # produces the default pink and blue but the split is at ~5.5
>>>
>>> # do something clever here
>>> # e.g., my.colors <- <create a palette that splits at zero>
>>>
>>> levelplot(z ~ x*y,grid,col.regions=my.colors)  # so there should be some light pink at the bottom and the rest increasingly intense blue
>>>
>>> Ideas appreciated.  Thanks in advance.
>>>
>>>
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>>
>> (650) 467-7374
>
> Don McKenzie
> Research Ecologist
> Pacific Wildland Fire Science Lab
> US Forest Service
>
> Affiliate Professor
> School of Environmental and Forest Sciences
> University of Washington
> [hidden email]
>
>
>

Don McKenzie
Research Ecologist
Pacific Wildland Fire Science Lab
US Forest Service

Affiliate Professor
School of Environmental and Forest Sciences
University of Washington
[hidden email]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: specify breaks in divergent palette in RColorBrewer: was divergent colors around zero in levelplot()

Achim Zeileis-4
In reply to this post by C. Alina Cansler
On Mon, 25 Nov 2013, C. Alina Cansler wrote:

> Don,
>
> This looks helpful:
> https://stat.ethz.ch/pipermail/r-help/2011-March/272361.html
>
> Also, here is some code that I had, and tried to make applicable to your
> question:
>
> div.colors <-colorRampPalette(c("blue", "white", "red" ))
> x<-seq(-1,12,1);x
> palette(div.colors(length(x)))
> y<- rep(1,length(x))
> barplot(y,  col=x+2, space=0, axes=FALSE, border = NA  ,   cex.names=1.3 ,
> xlab="", ylab="",xaxt="n")

These colors are not very well balanced because "blue" is much darker than
"red". You can see that more clearly when you desaturate the colors where
the blue branch corresponds to darker colors than the red branch.

The palettes in RColorBrewer or colorspace offer better balanced palettes.
Consider the code below.

And as for the original question: diverge_hcl(99) would be one option to
obtain 99 divergent colors in "colorspace".

library("colorspace")
pal <- function(col, border = "light gray") {
   n <- length(col)
   plot(0, 0, type="n", xlim = c(0, 1), ylim = c(0, 1),
     axes = FALSE, xlab = "", ylab = "")
   rect(0:(n-1)/n, 0, 1:n/n, 1, col = col, border = border)
}

par(mfrow = c(2, 2), mar = rep(1, 4))
pal(div.colors(9))
pal(diverge_hcl(9))
pal(desaturate(div.colors(9)))
pal(desaturate(diverge_hcl(9)))


> -Alina
>
>
>
>
> On Mon, Nov 25, 2013 at 4:27 PM, Don McKenzie <[hidden email]> wrote:
>
>> Bert or anyone else familiar with RColorBrewer:
>>
>> Has anyone tried to accomplish with RColorBrewer what I asked about in my
>> original post (below)?
>>
>> Here is an example cribbed from the levelplot() help examples
>>
>> x <- seq(pi/4, 5 * pi, length.out = 100)
>> y <- seq(pi/4, 5 * pi, length.out = 100)
>> r <- as.vector(sqrt(outer(x^2, y^2, "+")))
>> grid <- expand.grid(x=x, y=y)
>> grid$z <- cos(r^2) * exp(-r/(pi^3))
>>
>> # now use RColorBrewer to get a palette
>>
>> library("RColorBrewer?)
>> levelplot(z~x*y, grid,col.regions=brewer.pal(6,"BrBG?))   # the numeric
>> argument to brewer.pal is the number of colors used ? I tried several
>>
>> This gives me a nice brown-to-green gradient but does not (AFAICS) give me
>> control over where the center of the divergence lies. Even in this
>> symmetrical
>> example, I can?t get it to be at zero ? it repeats on either side of zero.
>>
>> thanks to anyone who pages through all this and makes a suggestion, even
>> if it doesn?t work.  :-)
>>
>> On Nov 22, 2013, at 10:25 PM, Bert Gunter <[hidden email]> wrote:
>>
>>> Use the Rcolorbrewer package.
>>>
>>> -- Bert
>>>
>>> On Fri, Nov 22, 2013 at 8:43 PM, Don McKenzie <[hidden email]>
>> wrote:
>>>> I would like to produce a levelplot with divergent colors such that
>> increasingly negative values of Z get darker in the first color and
>> increasingly
>>>> positive values get darker in the second color.  this is common in
>> cartography. I have tried tinkering with the col.regions argument but the
>> best I can do
>>>> is to get the split in the middle of my range of Z, but in my
>> particular case range(Z) is (-1,12).
>>>>
>>>> I am using R 3.0.2 on OSX 10.9
>>>>
>>>> Here is an example
>>>>
>>>> x <- y <- c(1:25)
>>>> grid <- expand.grid(x=x,y=y)
>>>> grid$z <- sort(runif(625,min=-1,max=12))
>>>> levelplot(z ~ x*y,grid)   # produces the default pink and blue but the
>> split is at ~5.5
>>>>
>>>> # do something clever here
>>>> # e.g., my.colors <- <create a palette that splits at zero>
>>>>
>>>> levelplot(z ~ x*y,grid,col.regions=my.colors)  # so there should be
>> some light pink at the bottom and the rest increasingly intense blue
>>>>
>>>> Ideas appreciated.  Thanks in advance.
>>>>
>>>>
>>>
>>> Bert Gunter
>>> Genentech Nonclinical Biostatistics
>>>
>>> (650) 467-7374
>>
>> Don McKenzie
>> Research Ecologist
>> Pacific Wildland Fire Science Lab
>> US Forest Service
>>
>> Affiliate Professor
>> School of Environmental and Forest Sciences
>> University of Washington
>> [hidden email]
>>
>>
>>
>>
>
>
> --
> C. Alina Cansler
> Fire and Mountain Ecology Lab
> School of Environmental and Forest Sciences
> College of the Environment
> University of Washington
> E-mail: [hidden email]
> Cell: 206-794-1630
>
> [[alternative HTML version deleted]]
>
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: specify breaks in divergent palette in RColorBrewer: was divergent colors around zero in levelplot()

barry rowlingson
In reply to this post by Don McKenzie-2
On Tue, Nov 26, 2013 at 1:02 AM, C. Alina Cansler <[hidden email]> wrote:
> Don,
>
> This looks helpful:
> https://stat.ethz.ch/pipermail/r-help/2011-March/272361.html

Yes, he's a helpful chap.

 The fundamental problem here is the colour palette. When I was a boy
all we had was a pen plotter with four coloured pens, and of course
you could stick different coloured pens in the different pen slots and
draw four different coloured lines. So your graphics package just said
which number pen it was going to use and the colour that came out was
up to you. Most R graphics functions still have this concept, although
there may be over 100 pens and you don't end up swearing when one runs
out of ink. A numeric value is converted to an integer and the integer
does a lookup in a palette to get the colour.

 What you really want, and what my colourscheme package did, was to
create functions that let users map values directly to colours. You
could then plot points and lines using that function which meant
totally controlled value-to-colour mappings that could be used across
different plots if desired. That worked because points and lines lets
you specify a colour value directly using something like col="#FF23EC"
(as well as allowing col=23 and doing a palette lookup).

 But the image function (and probably levelplot) doesn't allow that so
there's various tricks to make functional colour lookups work. I would
convert the image matrix values to a matrix of colours, then create a
matrix of the values 1:(n*m), and then image() that 1:(n*m) matrix
using the colour matrix as a palette. That way each cell had its own
palette entry, and you controlled that colour using the value-colour
function.

 Or you could just use ggplot which I'm pretty sure has the same
concept of mapping values to colours.

Barry

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: specify breaks in divergent palette in RColorBrewer: was divergent colors around zero in levelplot()

Prof Brian Ripley
On 26/11/2013 08:25, Barry Rowlingson wrote:

> On Tue, Nov 26, 2013 at 1:02 AM, C. Alina Cansler <[hidden email]> wrote:
>> Don,
>>
>> This looks helpful:
>> https://stat.ethz.ch/pipermail/r-help/2011-March/272361.html
>
> Yes, he's a helpful chap.
>
>   The fundamental problem here is the colour palette. When I was a boy
> all we had was a pen plotter with four coloured pens, and of course
> you could stick different coloured pens in the different pen slots and
> draw four different coloured lines. So your graphics package just said
> which number pen it was going to use and the colour that came out was
> up to you. Most R graphics functions still have this concept, although
> there may be over 100 pens and you don't end up swearing when one runs
> out of ink. A numeric value is converted to an integer and the integer
> does a lookup in a palette to get the colour.
>
>   What you really want, and what my colourscheme package did, was to
> create functions that let users map values directly to colours. You
> could then plot points and lines using that function which meant
> totally controlled value-to-colour mappings that could be used across
> different plots if desired. That worked because points and lines lets
> you specify a colour value directly using something like col="#FF23EC"
> (as well as allowing col=23 and doing a palette lookup).
>
>   But the image function (and probably levelplot) doesn't allow that so

Mis-information alert!   The help says

      col: a list of colors such as that generated by ‘rainbow’,
           ‘heat.colors’, ‘topo.colors’, ‘terrain.colors’ or similar
           functions.

and look at what they generate.  Or see e.g. ?col2rgb .

Although base graphics has the concept of a palette of colours, AFAIK it
has always been bolted on top of a general colour specification,
originally RGB and for many years already RGBA.

> there's various tricks to make functional colour lookups work. I would
> convert the image matrix values to a matrix of colours, then create a
> matrix of the values 1:(n*m), and then image() that 1:(n*m) matrix
> using the colour matrix as a palette. That way each cell had its own
> palette entry, and you controlled that colour using the value-colour
> function.
>
>   Or you could just use ggplot which I'm pretty sure has the same
> concept of mapping values to colours.
>
> Barry
>
> ______________________________________________
> [hidden email] 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.
>


--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: specify breaks in divergent palette in RColorBrewer: was divergent colors around zero in levelplot()

barry rowlingson
In reply to this post by barry rowlingson
On Tue, Nov 26, 2013 at 9:58 AM, Prof Brian Ripley
<[hidden email]> wrote:

>>   But the image function (and probably levelplot) doesn't allow that so
>
> Mis-information alert!   The help says
>
>       col: a list of colors such as that generated by ‘rainbow’,
>            ‘heat.colors’, ‘topo.colors’, ‘terrain.colors’ or similar
>            functions.
>
> and look at what they generate.  Or see e.g. ?col2rgb .
>
> Although base graphics has the concept of a palette of colours, AFAIK it
> has always been bolted on top of a general colour specification,
> originally RGB and for many years already RGBA.


 Yes image allows you to specify col=, but it always specifies a
palette. The matrix values are scaled from 1:length(col) and looked up
in that palette. You can't call image with z as matrix of colours and
get those colours, nor set col to a matrix of colours and a see those
colours laid out. This is unlike points() where specifying col= as a
vector of the same length as the number of points gives you a 1:1
mapping of points to colours.

 To do image() with a 1:1 mapping of cell values to colours requires a
tiny bit of hoop-jumping.

Barry

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: specify breaks in divergent palette in RColorBrewer: was divergent colors around zero in levelplot()

Don McKenzie-2
Thanks to everyone who weighed in on this.  I found a naive solution that was good enough for my needs, and it may take me a bit to get the subtleties
of your comments.

On Nov 26, 2013, at 2:12 AM, Barry Rowlingson <[hidden email]> wrote:

> On Tue, Nov 26, 2013 at 9:58 AM, Prof Brian Ripley
> <[hidden email]> wrote:
>
>>>  But the image function (and probably levelplot) doesn't allow that so
>>
>> Mis-information alert!   The help says
>>
>>      col: a list of colors such as that generated by ‘rainbow’,
>>           ‘heat.colors’, ‘topo.colors’, ‘terrain.colors’ or similar
>>           functions.
>>
>> and look at what they generate.  Or see e.g. ?col2rgb .
>>
>> Although base graphics has the concept of a palette of colours, AFAIK it
>> has always been bolted on top of a general colour specification,
>> originally RGB and for many years already RGBA.
>
>
> Yes image allows you to specify col=, but it always specifies a
> palette. The matrix values are scaled from 1:length(col) and looked up
> in that palette. You can't call image with z as matrix of colours and
> get those colours, nor set col to a matrix of colours and a see those
> colours laid out. This is unlike points() where specifying col= as a
> vector of the same length as the number of points gives you a 1:1
> mapping of points to colours.
>
> To do image() with a 1:1 mapping of cell values to colours requires a
> tiny bit of hoop-jumping.
>
> Barry
>
> ______________________________________________
> [hidden email] 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.

Don McKenzie
Research Ecologist
Pacific Wildland Fire Science Lab
US Forest Service

Affiliate Professor
School of Environmental and Forest Sciences
University of Washington
[hidden email]

______________________________________________
[hidden email] 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.