[R] Ploting graphics using X tints from a color

hadley wickham h.wickham at gmail.com
Tue Dec 13 21:45:04 CET 2005


> I'm trying to draw a 2D plot using multiple tints of red. The
> (simplified) setup is the following: || year | x | y ||

You might find this function useful:

map_colour_gradient <- function(x, low="red",
mid="white",high="black", midpoint = 0) {
	x <- as.numeric(x)
	low.rgb <- col2rgb(low, TRUE)/256
	mid.rgb <- col2rgb(mid, TRUE)/256
	high.rgb <- col2rgb(high, TRUE)/256

	interp_r <- approxfun(c(min(x, na.rm=TRUE), midpoint, max(x,
na.rm=TRUE)), c(low.rgb[1], mid.rgb[1], high.rgb[1]))
	interp_g <- approxfun(c(min(x, na.rm=TRUE), midpoint, max(x,
na.rm=TRUE)), c(low.rgb[2], mid.rgb[2], high.rgb[2]))
	interp_b <- approxfun(c(min(x, na.rm=TRUE), midpoint, max(x,
na.rm=TRUE)), c(low.rgb[3], mid.rgb[3], high.rgb[3]))

	
	rgb(interp_r(x), interp_g(x), interp_b(x))
}

Given a numeric vector x, it will create a smooth gradient (linearly
through RGB space).

eg.

x <- rnorm(100)
y <- rnorm(100)

plot(x, y, col=map_colour_gradient(x), pch=20)
plot(x, y, col=map_colour_gradient(x, low="black", high="black",
mid="yellow"), pch=20)

Note, however, using colour is only likely to find the most prominent
of patterns.

Hadley




More information about the R-help mailing list