Eric Harley
2006-Oct-06 20:15 UTC
[R] is it possible to fill with a color or transparency gradient?
Hi all, Is there a way to fill a rectangle or polygon with a color and/or transparency gradient? This would be extremely useful for me in terms of adding some additional information to some plots I'm making, especially if I could define the gradient on my own by putting functions into rgb something like rgb( r=f(x,y), g=f(x,y), b=f(x,y), alpha=f(x,y) ). Not so important whether the coordinates are in terms of the plot axes or normalized to the polygon itself somehow. Ideally it would work not only for a fill color but also for shading lines. I haven't been using R very long, so it's possible that I'm just missing something, but I haven't found anything like this in the help files. I've tried to poke around in graphics, grid, and ggplot, without any luck so far. I really like some of the functionality in ggplot, and it does some nice things with continuous gradients for the color of scatter plot points, for example, but it each individual point (or grob) is always one solid color as far as I can tell. Thanks, Eric
Marc Schwartz (via MN)
2006-Oct-06 20:25 UTC
[R] is it possible to fill with a color or transparency gradient?
On Fri, 2006-10-06 at 16:15 -0400, Eric Harley wrote:> Hi all, > > Is there a way to fill a rectangle or polygon with a color and/or > transparency gradient? This would be extremely useful for me in terms > of adding some additional information to some plots I'm making, > especially if I could define the gradient on my own by putting > functions into rgb something like rgb( r=f(x,y), g=f(x,y), b=f(x,y), > alpha=f(x,y) ). Not so important whether the coordinates are in terms > of the plot axes or normalized to the polygon itself somehow. Ideally > it would work not only for a fill color but also for shading lines. > > I haven't been using R very long, so it's possible that I'm just > missing something, but I haven't found anything like this in the help > files. I've tried to poke around in graphics, grid, and ggplot, > without any luck so far. I really like some of the functionality in > ggplot, and it does some nice things with continuous gradients for the > color of scatter plot points, for example, but it each individual > point (or grob) is always one solid color as far as I can tell. > > Thanks, > EricTake a look at the gradient.rect() function in Jim Lemon's 'plotrix' CRAN package. HTH, Marc Schwartz
hadley wickham
2006-Oct-06 21:51 UTC
[R] is it possible to fill with a color or transparency gradient?
> I haven't been using R very long, so it's possible that I'm just > missing something, but I haven't found anything like this in the help > files. I've tried to poke around in graphics, grid, and ggplot, > without any luck so far. I really like some of the functionality in > ggplot, and it does some nice things with continuous gradients for the > color of scatter plot points, for example, but it each individual > point (or grob) is always one solid color as far as I can tell.ggplot supports alpha blending provided your graphics device does too (Quartz on the mac, or pdf version 1.3 or later). Have a look at the alpha function for easily making transparent colours, as well as scgradient for specifying the colour scale. Oh, I see you want a gradient fill on a polygon - that's a lot harder, and I can only suggest dividing your polygon up into smaller rectangles and filling those individually. That appears to be what gradient.rect does. Hadley
Jim Lemon
2006-Oct-08 23:51 UTC
[R] is it possible to fill with a color or transparency gradient?
Eric Harley wrote:> Hi all, > > Is there a way to fill a rectangle or polygon with a color and/or > transparency gradient? This would be extremely useful for me in terms > of adding some additional information to some plots I'm making, > especially if I could define the gradient on my own by putting > functions into rgb something like rgb( r=f(x,y), g=f(x,y), b=f(x,y), > alpha=f(x,y) ). Not so important whether the coordinates are in terms > of the plot axes or normalized to the polygon itself somehow. Ideally > it would work not only for a fill color but also for shading lines. > > I haven't been using R very long, so it's possible that I'm just > missing something, but I haven't found anything like this in the help > files. I've tried to poke around in graphics, grid, and ggplot, > without any luck so far. I really like some of the functionality in > ggplot, and it does some nice things with continuous gradients for the > color of scatter plot points, for example, but it each individual > point (or grob) is always one solid color as far as I can tell. >Hi Eric, You can fill a rectangle with a "gradient" (slices of colors) using gradient.rect in the plotrix package. Filling an arbitrary polygon would be a lot harder to program. You may also be interested in the color.scale function that assigns colors to numerical values. Jim