val2col | R Documentation |
The val2col
function converts a vector of values("z")
to a vector of color levels. One must define the number of colors.
The limits of the color scale ("zlim") or the break points for the
color changes("breaks") can also be defined. When breaks and zlim are
defined, breaks overrides zlim. All arguments are similar to those in the
image
function.
val2col(z, zlim, col = hcl.colors(12, "YlOrRd", rev = TRUE), breaks)
z |
A vector of values (default is 12 colors from the |
zlim |
Limits of the color scale values. |
col |
Vector of color values |
breaks |
Break points for color changes. If breaks is specified then |
set.seed(1) n <- 250 x <- seq(n) y <- rnorm(n) # Use all levels, evenly distributed breaks Col <- val2col(y, col=rainbow(20)) plot(x,y, pch=21, bg=Col) # Use limits, evenly distributed breaks pal <- colorRampPalette(c("blue", "cyan", "yellow", "red")) Col <- val2col(y, zlim=c(-1,1), col=pal(20)) plot(x,y, pch=21, bg=Col) abline(h=c(-1,1), col=8, lty=2) # Use custom breaks (break vector must have one more break than color) Col <- val2col(y, col=topo.colors(6), breaks=c(-Inf, -2, -1, 0, 1, 2, Inf)) plot(x,y, pch=21, bg=Col) abline(h=c(-Inf, -2, -1, 0, 1, 2, Inf), col=8, lty=2) # by default, image() and val2col() use the same palette grd <- expand.grid(x = seq(0, 1, length.out = nrow(volcano)), y = seq(0, 1, length.out = ncol(volcano))) grd$z <- c(volcano) image(volcano) points(grd, pch = 20, col = val2col(grd$z), cex = 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.