[R] grid.polygon() + color gradient
baptiste auguie
baptiste.auguie at gmail.com
Thu Jun 25 11:29:14 CEST 2009
Hi,
I don't think the fill parameter can be a colour gradient. You'll need
to create small polygons, each with its own fill (200, say). Try this,
x= c(0, 0.5, 1)
y= c(0.5, 1, 0.5)
grid.polygon(x=x, y=y, gp=gpar(fill="grey90", col="grey90"))
xx <- seq(range(x)[1],range(x)[2], length=100)
yy <- rep(max(y), length(xx))
cols <- colorRampPalette(c("green", "lightgray"))(length(xx))
for(ii in seq_along(xx[-length(xx)])) {
grid.clip(x=xx[ii], y=0.5,
width= xx[ii+1],
height=1,
just="bottom")
grid.polygon(x=c(0, 0.5, 1), y=c(0.5, 1, 0.5), gp=gpar(fill=cols[ii],
col=NA))
}
Note that the situation would become rather more complicated for a
gradient at some angle (see ?grobX if you need to).
If you're free to choose an external tool to produce this, the TeX
package Tikz has good support for gradients and clipping.
HTH,
baptiste
Kexin Ji wrote:
> Hi,
>
> I wonder whether there is a way to generate a polygon (a triangle in
> my case) with color gradient using grid.polygon() in package grid?
>
> I tried something like
>
> library(grid)
> grid.polygon(x=c(0, 0.5, 1), y=c(0.5, 1, 0.5), gp=gpar(col=NA,
> fill=colorRampPalette(c("green", "lightgray"),
> space="Lab")(200)))
>
> But am only getting a triangle filled with color green, whereas the
> aim is a triangle of color gradient from green to lightgray.
>
> Can grid.polygon() generate a color gradient, or am I being mistaken?
>
> Best to my knowledge, is it true that R currently doesn't contain any
> other function that might generate a polygon with color gradient?
>
> Thank you!
>
> Kexin
>
>
> [[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.
>
>
--
_____________________________
Baptiste Auguié
School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
Phone: +44 1392 264187
http://newton.ex.ac.uk/research/emag
More information about the R-help
mailing list