[R] How to draw a transparent polygon

William Dunlap wdunlap at tibco.com
Wed May 21 18:05:21 CEST 2014


You can use adjustcolor() to do some of the arithmetic for you.  E.g.,
the following draws red squares with 10 opacities ('alpha's) from 5%
to 95%:
   plot(1:10)
   square <- 0.45 * cbind(c(-1,1,1,-1),c(-1,-1,1,1))
   for(i in 1:10) polygon(square+i, col=adjustcolor("red", alpha=(i-.5)/10))
Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Wed, May 21, 2014 at 8:47 AM, David L Carlson <dcarlson at tamu.edu> wrote:
> The standard colors in R are opaque, but you can add an alpha value to make them semi-transparent. In this example we set alpha halfway between 0 and 255 to define a semi-transparent red:
>
>> set.seed(42)
>> x <- runif(10)*10
>> y <- runif(10)*10
>> plot(x, y, pch=16)
>> col2rgb("red", alpha=TRUE)
>       [,1]
> red    255
> green    0
> blue     0
> alpha  255
>> redtrans <- rgb(255, 0, 0, 127, maxColorValue=255)
>> polygon(c(2, 5, 8), c(2, 10, 2), col=redtrans)
>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
>
>
>
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jun Shen
> Sent: Wednesday, May 21, 2014 10:32 AM
> To: R-help
> Subject: [R] How to draw a transparent polygon
>
> Hi everyone,
>
> How do I draw a transparent ploygon overlaying with a scatter plot?
>
> Let's say, we call plot() to have a scatter plot, then call polygon() to
> add a polygon. I was hoping the polygon can be transparent so the scatter
> plot is still visible. I can't find any argument in polygon() for such a
> feature. Is there another way to do it? Thanks.
>
> Jun Shen
>
>         [[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.
>
> ______________________________________________
> 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