Re: [R] Ploting graphics using X tints from a color

From: hadley wickham <h.wickham_at_gmail.com>
Date: Wed 14 Dec 2005 - 07:45:04 EST

> 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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Dec 14 09:08:43 2005

This archive was generated by hypermail 2.1.8 : Wed 14 Dec 2005 - 14:31:31 EST