[R] grid.polygon() + color gradient

baptiste auguie baptiste.auguie at gmail.com
Thu Jun 25 11:49:01 CEST 2009


[I neglected to check some details in the previous post]

This one should work better,

library(grid)

x= c(0, 0.5, 1)
y= c(0.5, 1, 0.5)
grid.polygon(x=x, y=y, gp=gpar(fill=NA, col="grey90")) # outer shell

xx <- seq(range(x)[1],range(x)[2], length=100)
dx <- diff(xx) # width of clipped triangles
cols <- colorRampPalette(c("green", "lightgray"))(length(dx))

for(ii in seq_along(xx[-length(xx)])){
grid.clip(x=xx[ii], y=0.5,
width= 1.2*dx[ii], # fudge factor to overlap well
height=1,
just="bottom")
# if(ii%%2)# testing with every other masked
grid.polygon(x=c(0, 0.5, 1), y=c(0.5, 1, 0.5), gp=gpar(fill=cols[ii], 
col=cols[ii]))
}





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