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]]
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
[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
Glad if it helps. check out this page of examples for tikz, http://www.texample.net/tikz/examples/feature/shadings/ If you do choose this route, you could perhaps read the new wiki page on importing graphics in a R plot, http://wiki.r-project.org/rwiki/doku.php?id=tips:graphics-misc:display-images (you'll want to convert the result of Tikz into a bitmap format first, with imagemagick for example). As for the pure R graphics solution, you'll have to play with Grid viewports (and perhaps the gridBase package if your "Figure" is using base graphics). Can you post a minimal, self-contained, reproducible example of what you're trying to achieve to the list? baptiste 2009/6/25 Kexin Ji <kexinj@gmail.com>> Hi, > > The triangle looks great!! > And thanks for mentioning the TeX package Tikz! Maybe I'll check it out > later. > The only problem is that I need to append this color-gradient triangle into > a another Figure I'm working on. But when I try to do that, this wonderful > triangle overwrites the other one. Have tried to append it with not much > luck.. > Much appreciation to your help though!!! > > Kexin > > > On 6/25/09, baptiste auguie <baptiste.auguie@gmail.com> wrote: >> >> 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@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 >> ______________________________ >> >> >-- _____________________________ 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 ______________________________ [[alternative HTML version deleted]]