所有,我不确定这个例子出了什么问题:
library(ggplot2)
par(ask = TRUE)
alphaVals <- seq(0.1, 1, 0.1)
for(alpha in alphaVals) print(qplot(x = 1:100, y = 1:100) + geom_rect(xmin = 20, xmax = 70, ymin = -Inf, ymax = Inf, alpha = alpha, fill = 'grey50'))
你可以看到从alpha等于0到大约0.2我得到了一些透明度,但之后它就消失了。我之前从未遇到过设置ggplot2图层的alpha刻度的问题。
我在这里做错了什么?
干杯, 亚伦
答案 0 :(得分:3)
这里的问题是ggplot2在同一位置绘制了100次矩形。因此,100个堆叠的透明形状表现为单个不透明形状。我通过使用Adobe Illustrator检查pdf输出来发现这一点。我在下面提供了一个可能的解决方案(重写为使用ggplot语法而不是qplot)。我当然觉得这种行为是出乎意料的,但我不确定它是否应该被称为错误。
我提出的解决方案涉及(1)将矩形数据放在自己的data.frame中,以及(2)在每个层中分别指定数据(但不在ggplot()
调用中)。
library(ggplot2)
dat = data.frame(x=1:100, y=1:100)
rect_dat = data.frame(xmin=20, xmax=70, ymin=0, ymax=100)
# Work-around solution.
p = ggplot() +
geom_point(data=dat, aes(x=x, y=y)) +
geom_rect(data=rect_dat,
aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
alpha = 0.3, fill = "black")
ggsave("test.png", plot=p, height=5, width=5, dpi=150)
# Original version with 100 overlapping rectangles.
p2 = ggplot(dat, aes(x=x, y=y)) +
geom_point() +
geom_rect(xmin=20, xmax=70, ymin=0, ymax=100, alpha=0.01, fill="black")
ggsave("test.pdf", height=7, width=7)
答案 1 :(得分:2)