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

放肆的年华 提交于 2019-11-30 20:40:23

Given that you want disks with constant colour & opacity simplest thing to do that fixed it for me, also in the RStudio plot preview window is just to use option 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'))

Alternatively, shape=21 and a 100% semitransparent fill with fill=adjustcolor("dodgerblue",alpha.f=0) also works:

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'))

Using stroke=0 as suggested in the currently accepted answer doesn't seem to resolve the problem entirely for me (ringing effect goes away a little bit but not entirely, this is on Windows at least) :

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'))

Changing stroke to 0 seems to have hte desired result:

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'))

Update: Tom Wenseleers solution (accepted) is better than the below.

After discussion with @42, the solution is that the PNG default had resolution low enough that at the border between a marker and the image background there was a blending artifact (might not be the right terminology).

Increasing the dpi solves the issue, and adding stroke=0 looks a bit better.

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!