ggplot alpha 水平在点的填充和边界上显得不同(振铃假象)

ggplot alpha levels appear different on fill and border of points (ringing artefact)

我正在使用 ggplot 绘制许多点,所有点的透明度值都是恒定的。

我发现圆形点的填充比它们各自的边界更透明,因此边界明显比它们的填充更亮(我在深色背景上绘制浅色圆圈),即似乎是一个响亮的人工制品。

The effect is that they look like rings rather than semi-transparent circles.

library(ggplot2)
set.seed(123)
data <- data.frame( x = sample(1:100,2000, replace=T), 
                    y = sample(1:100,2000, replace=T) )
ggplot(data, aes(x,y)) + 
  geom_point(alpha=0.2, color="dodgerblue", fill="dodgerblue", size=4) +
  theme(panel.background = element_rect(fill = 'black', colour = 'black'))

我不确定为什么会这样,所以关于为什么会发生这种情况的信息会很好。

可能的解决方案是使边框和填充具有相同的透明度,或者使边框 100% 透明(将边框设置为背景色,当点重叠时会破坏视觉效果)。我不确定该怎么做。

将 stroke 更改为 0 似乎得到了预期的结果:

ggplot(data, aes(x,y)) + 
  geom_point(alpha=0.2, colour="dodgerblue", fill=mycol, stroke=0,  size=5) +
  theme(panel.background = element_rect(fill = 'black', colour = 'black'))

更新: Tom Wenseleers 解决方案(已接受)比以下更好。

在与@42 讨论后,解决方案是 PNG 默认分辨率足够低,以至于在标记和图像背景之间的边界处存在混合伪影(可能不是正确的术语)。

增加 dpi 可以解决问题,添加 stroke=0 看起来会好一些。

ggsave("plot.png",
  ggplot(data, aes(x,y)) + 
  geom_point(alpha=0.2, color="dodgerblue", fill="dodgerblue", size=4, stroke=0) +
  theme(panel.background = element_rect(fill = 'black', colour = 'black')),
  dpi=1200)

鉴于您想要具有恒定颜色和不透明度的磁盘,最简单的方法就是为我修复它,同样在 RStudio 绘图预览中 window 只是使用选项 shape=16 :

data <- data.frame( x = sample(1:100,2000, replace=T), 
                y = sample(1:100,2000, replace=T) )
ggplot(d, aes(x,y)) + 
  geom_point(alpha=0.2, color="dodgerblue", size=5, shape=16) +
  theme(panel.background = element_rect(fill = 'black', colour = 'black'))

或者,shape=21 和 100% 半透明填充 fill=adjustcolor("dodgerblue",alpha.f=0) 也有效:

ggplot(data, aes(x,y)) + 
     geom_point(alpha=0.2, fill=adjustcolor("dodgerblue",alpha.f=0), size=5, shape=21) +
     theme(panel.background = element_rect(fill = 'black', colour = 'black'))

按照当前接受的答案中的建议使用 stroke=0 似乎并不能完全解决我的问题(振铃效果稍微消失但不完全,这是在 Windows 上最少):

ggplot(data, aes(x,y)) + 
    geom_point(alpha=0.2, colour="dodgerblue", fill="dodgerblue", stroke=0,  size=5) +
    theme(panel.background = element_rect(fill = 'black', colour = 'black'))