[R] How to draw a transparent polygon

Bert Gunter gunter.berton at gene.com
Wed May 21 20:45:37 CEST 2014


Inline.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch




On Wed, May 21, 2014 at 11:26 AM, Boris Steipe <boris.steipe at utoronto.ca> wrote:
> Hex-codes are ubiquitously used on the Web - they are a short-hand version of specifying rgb triplets. The first two digits specify the red value, the second two are for green, then blue. R accepts a fourth pair of digits to optionally specify the transparency, the semantics of the code is thus "#RRGGBB" or "#RRGGBBAA".
>
> I prefer hex codes because they are compact, and convenient to make up colors on the fly. But there are many ways to define colors rigorously and then convert them.
>
> E.g. if you go with one of the internally defined "named" colors:
> R provides col2rgb() to convert different types of colors to rgb values, and rgb() to convert triplets of rgb values to hex-code. (Since col2rgb creates rows with values between 0 and 255, and rgb expects columns with intensities from 0 to 1, you have to transpose and divide).

Not strictly true. There's a maxColorValue argument to rgb, so instead
of dividing and using the default of 1, you can not divide and and
specify the max:

> rgb(t(col2rgb("red")),max=255)
[1] "#FF0000"

> rgb(t(col2rgb("peachpuff")),max=255)
[1] "#FFDAB9"




>
> rgb(t(col2rgb("red"))/255)        # "#FF0000"
> rgb(t(col2rgb("peachpuff"))/255)  # "#FFDAB9"
>
> But the real advantage is when using color matching and palette tools like:
>    https://kuler.adobe.com/
>
> Or you can extract color palettes from images like here:
>    http://www.pictaculous.com/
>
> Or you can use the "eye-dropper" tool of standard image editing software to access to color value of a particular pixel, or online here ...
>    http://html-color-codes.info/colors-from-image/
>    http://imagecolorpicker.com/
>
> Many options.
> Hope this helps,
> Boris
>
>
>
> On 2014-05-21, at 1:59 PM, Jun Shen wrote:
>
>> Thanks everyone who replied.
>>
>> Boris, could you explain a bit more how to obtain this hex-code for a specific color? Thanks.
>>
>> Jun
>>
>>
>> On Wed, May 21, 2014 at 1:37 PM, Boris Steipe <boris.steipe at utoronto.ca> wrote:
>> ... 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.
>>
>> ______________________________________________
>> 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