[R] Scatterplot with the 3rd dimension = color?

Ben Bolker bbolker at gmail.com
Mon Oct 3 04:42:03 CEST 2011

Duncan Murdoch <murdoch.duncan <at> gmail.com> writes:

> On 11-10-02 1:11 PM, Kerry wrote:
> > I have 3 columns of data and want to plot each row as a point in a
> > scatter plot and want one column to be represented as a color gradient
> > (e.g. larger  values being more red). Anyone know the command or
> > package for this?
> It's not a particularly effective display, but here's how to do it.  Use 
> rainbow(101) in place of rev(heat.colors(101)) if you like.
> x <- rnorm(10)
> y <- rnorm(10)
> z <- rnorm(10)
> colors <- rev(heat.colors(101))
> zcolor <- colors[(z - min(z))/diff(range(z))*100 + 1]
> plot(x,y,col=zcolor)


d <- data.frame(x,y,z)

  I agree about the "not particularly effective display"
comment, but if you have two continuous predictors and
a continuous response you've got a tough display problem --
your choices are:

  1. use color, size, or some other graphical characteristic
(pretty far down on the "Cleveland hierarchy")
  2. use a perspective plot (hard to get the right viewing
angle, often confusing)
  3. use coplots/small multiples/faceting (requires
discretizing one dimension)

More information about the R-help mailing list