Reputation: 627
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'):
Upvotes: 3
Views: 420
Reputation: 3294
Found out that this is a windows-only bug if the polygon is bigger than the plot.
Links:
In the meantime you could do a workaround:
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)
par(mfrow=c(2,2))
plot(var2~var1)
polygon(
x = c(seq(min(var1),max(var1),0.1), rev(seq(min(var1),max(var1),0.1))),
y = c(rep(120, length(seq(min(var1),max(var1),0.1))), rep(100,length(seq(min(var1),max(var1),0.1)))),
col = adjustcolor(col = 'grey', alpha.f = 0.5),
border=NA
)
edit: oh just realized that you've also found the bug description.
Upvotes: 1
Reputation: 627
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.
Upvotes: 1