[R] graphs with gradients of colors

Greg Snow Greg.Snow at intermountainmail.org
Tue Sep 18 22:09:04 CEST 2007


The first question you should ask is "why do you want to do this?".
Adding gradients and other things like that can make a graph look neat,
but can also distort the information in the graph.  You should carefully
consider whether doing things like this really help the graph, or
distract from it.

If you do create a graph with the gradient, you should also create one
without the gradient and compare them to see which conveys the important
information better.

If you decide to go ahead with this, there was a discussion a while ago
on including pictures as the background of bar charts, you could use
something similar to that but with a gradient instead of a picture.  For
some cases it may be easier to create your graph, then use another tool
like imagemagick or gimp to replace a color with the gradient.

If you want to do it in R, then here are a couple of options to get you
started (your specifics will vary depending on the curve you want to
use, how smooth you want things to look, etc.

xx <- seq( -3,3, length=250 )
yy <- dnorm(xx, 0, 1)

plot(xx,yy, type='l')
tmp <- par('usr')

par(new=TRUE)

image( tmp[1:2], seq( tmp[3], tmp[4], len=101 ), 
  matrix( 1:100, ncol=100, nrow=1), add=TRUE,
  col=heat.colors(100))

polygon( c(tmp[1],xx,tmp[2],tmp[2],tmp[1] ), 
  c(yy[1], yy, yy[ length(yy) ], tmp[4], tmp[4] ), 
  col='white', border='white')
polygon( c(tmp[1], tmp[2], tmp[2], tmp[1] ),
  c(yy[1],yy[1], tmp[3], tmp[3]), col='white', border='white')

lines(xx,yy)
box()

# this one may be a bit of overkill for most cases, but does give more
# flexibility

library(TeachingDemos)

xx <- seq(0, 2*pi, length=100)
yy <- sin(xx)

plot(xx,yy, type='l', ylim=c(-1.1, 1.1))

tmpfun <- function(...){
   image( c(0,2*pi), seq(-1.1,1.1,length=101), matrix( 1:100, nrow=1 ),
	col=terrain.colors(100), add=TRUE )
}

xxx <- embed(xx,2)
for( i in 1:99 ){
	clipplot( tmpfun(), c(xxx[i,2],xxx[i,1]), c(-1.1,
(yy[i]+yy[i+1])/2 ) )
}

lines(xx,yy)

# shade from 0

plot(xx,yy, type='l', ylim=c(-1.1, 1.1))

tmpfun <- function(...){
   image( c(0,2*pi), seq(-1.1,1.1,length=101), matrix( 1:100, nrow=1 ),
	col=terrain.colors(100), add=TRUE )
}

xxx <- embed(xx,2)
for( i in 1:99 ){
	clipplot( tmpfun(), c(xxx[i,2],xxx[i,1]), c(0, (yy[i]+yy[i+1])/2
) )
}

lines(xx,yy)


Use at your own risk and definitly compare them to simple plots for
clarity.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111
 
 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Van Dongen Stefan
> Sent: Monday, September 17, 2007 7:37 AM
> To: R-help at stat.math.ethz.ch
> Subject: [R] graphs with gradients of colors
> 
> Hi All,
>  
> I would like to fill the area under a curve with a gradient 
> of colors. Are there any packages or trick I could use
>  
> Thanks
>  
> Stefan
>  
>  
> Stefan Van Dongen
> Antwerp
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list