[R] How to draw a transparent polygon

Boris Steipe boris.steipe at utoronto.ca
Wed May 21 19:37:36 CEST 2014


... I just specify alpha values directly in the hex-code for a color:

set.seed(112352)
plot(runif(20),runif(20), pch=16)
polygon(runif(3), runif(3), col="#FF000022")
polygon(runif(3), runif(3), col="#00FF0022")
polygon(runif(3), runif(3), col="#0000FF22")

B.


On 2014-05-21, at 12:05 PM, William Dunlap wrote:

> 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.
> 
> ______________________________________________
> 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