问题
I am plotting many points using ggplot with a constant transparency value for all points.
What I find is that the circular points have a more transparent fill than their individual border, so that the borders are noticeably brighter than their fill (I'm plotting light circles on a dark background), i.e. there seems to be a ringing artefact.
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'))
I'm not sure why it does this, so information as to why this occurs would be great.
Possible solutions would be to make the border and fill the same transparency, or to make the border 100% transparent (setting the border to say, the background colour, would ruin the visuals when points overlap). I'm not sure how to do either of these.
回答1:
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'))
回答2:
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'))
回答3:
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)
来源:https://stackoverflow.com/questions/37381684/ggplot-alpha-levels-appear-different-on-fill-and-border-of-points-ringing-artef