Skip to content
Back to formatted view

Raw Message

Message-ID: <f8e6ff050512131245v3d021a93h7b8d1e7c3f56f1ad@mail.gmail.com>
Date: 2005-12-13T20:45:04Z
From: Hadley Wickham
Subject: Ploting graphics using X tints from a color
In-Reply-To: <4c817d530512131034u3796d64ao6d45a4d94560ca88@mail.gmail.com>

> 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