Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistent polygon behavior using transparent colors

Tags:

r

I was trying to draw a partially transparent polygon recently (something I've done many times in the past) and discovered inconsistent behavior where, under certain conditions, the polygon fill color will not be drawn.

Running the following code, the third and fourth polygon chunks will work, but the first two will not (if you change border from NULL to a color, the border will draw just fine, but the fill will not).

var1 = c(10.13981, 11.47067, 11.07515, 11.32449, 11.57041, 11.75539, 10.81107, 10.90303, 10.47502,
         10.90169, 11.38179, 10.05446, 10.72442, 10.68973, 12.31730, 13.16385, 12.02199, 10.91423,
         11.54465, 10.80909)

var2 = c(116.0, 100.0, 117.0, 116.0, 112.0, 125.5, 110.0, 103.0,  94.0, 105.0, 97.0, 131.0, 108.5,  95.0, 96.0,  78.0,  89.0, 103.0,  99.0,  90.0)

plot(var2~var1)

#What I want to draw
polygon(
  x = c(seq(9,15,0.1), rev(seq(9,15,0.1))),
  y = c(rep(120,61), rep(100,61)),
  col = adjustcolor(col = 'grey', alpha.f = 0.5),
  border = NA
)

#checking if replacing "adjustcolor" with the output of the function works
polygon(
  x = c(seq(9,15,0.1), rev(seq(9,15,0.1))),
  y = c(rep(120,61), rep(100,61)),
  col = "#BEBEBE1A",
  border = NA
)

#works just fine if alpha.f is set to 1 (fully opaque)
polygon(
  x = c(seq(9,15,0.1), rev(seq(9,15,0.1))),
  y = c(rep(120,61), rep(100,61)),
  col = adjustcolor(col = 'grey', alpha.f = 1),
  border = NA
)

#works just fine with transparency using different x/y values
polygon(
  x = c(11,11,12,12),
  y = c(110,120,120,110),
  col = adjustcolor(col = 'grey', alpha.f = .1),
  border = NA
)

Essentially, certain combinations of x/y variables and alpha.f values will cause the polygon fill to just not work.

This makes no sense whatsoever to me. Am I missing something obvious? This is driving me crazy.

adding screenshots of my outputs using the various polygon codes (changed all borders to 'black'):

#1enter image description here

#2 enter image description here

#3enter image description here

#4enter image description here

like image 366
C. Denney Avatar asked Apr 25 '26 18:04

C. Denney


1 Answers

Upon searching the Upcoming Changes to R page, it turns out that this is a known bug when the polygon draws outside the plotting area with semi-transparent fills in windows graphics devices. It is marked as "fixed" but apparently not yet implemented. Hopefully this comes out relatively soon.

like image 111
C. Denney Avatar answered Apr 28 '26 07:04

C. Denney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!