最近我尝试绘制一个部分透明的多边形(这是我过去经常做的事情),但发现在某些情况下,多边形填充颜色无法绘制。
运行以下代码,第三和第四个多边形将工作正常,但前两个不会(如果你将border从NULL改为颜色,则边框会正常绘制,但填充不会)。
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
)
实际上,一些 x/y 变量和 alpha.f 值的特定组合会导致多边形填充无法正常工作。
这对我来说完全没有意义。我错过了什么明显的吗?这让我发疯。
添加使用不同多边形代码的输出屏幕截图(将所有边框更改为“黑色”):