[R] diagonally fill a rectangle with color gradient

Greg Snow Greg.Snow at imail.org
Wed Apr 9 20:57:09 CEST 2008


Here are a couple of ways using base graphics:

> library(TeachingDemos)
> x <- y <- seq(0,1,length.out=100)
> tmp <- expand.grid(x=x,y=y)
> z <- matrix( tmp$x + tmp$y, 100 )
>
> plot( 0:1, 0:1, type='n', xlab='x', ylab='y' )
> subplot( image(x,y,z, col=topo.colors(100), axes=FALSE, xlab='',
ylab=''),
+   c(0.2,0.4), c(0.2,0.7) )
>
> clipplot( image(x,y,z, col=topo.colors(100), add=TRUE),
+   c(0.7,0.9), c(0.3,0.8) )
>

In version 2.7 there is also the option to use the clip function
(instead of clipplot in TeachingDemos) like:

> clip( 0.3, 0.8, 0.9, 1.0 )
> image(x,y,z, col=topo.colors(100), add=TRUE)

However the help for "clip" notes that it can be hard to predict when
the clipping region will be reset, so this sometimes works (my tests had
it work if I did the above 2 lines after the subplot example) and
sometimes does not (my tests did not work when used after the clipplot
example, or on a new plot).

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.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 tom soyer
> Sent: Tuesday, April 08, 2008 12:24 PM
> To: r-help at r-project.org
> Subject: [R] diagonally fill a rectangle with color gradient
> 
> Hi,
> 
> Is it possible to diagonally fill a rectangle with a color 
> gradient? I noticed that the gradient.rect of plotrix could 
> fill a rect either up and down or from side to side. I am 
> looking for something similar but fills diagonally instead, 
> e.g., from the upper left corner to the bottom right.
> Does anyone know how to do it in R?
> 
> Thanks,
> 
> --
> Tom
> 
> 	[[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