Slight point strokes in ggplot points

2019-07-13 02:14发布

Consider the following:

library(ggplot2)

df = data.frame(x = rep(0,9), y = rep(0,9), alp = c(1:8/20,1))
ggplot(df) + 
  geom_point(aes(x, y, alpha=alp), size = 20, col = 'red') + 
  theme_minimal() + facet_wrap(~ alp) + guides(alpha = F)

enter image description here

As you can see there are feint outlines. It makes overlaying many low-transparency points look a bit like frogspawn. Is this just a Mac thing? Any idea how to remove it?

标签: r ggplot2
1条回答
狗以群分
2楼-- · 2019-07-13 02:35

The default point shape for ggplot2 is pch = 19. It's not one of those points where the colour of its border and its inside can be controlled separately; for instance, in the following, fill = 'black' has no effect.

library(ggplot2)

df = data.frame(x =runif(1000), y = runif(1000))

p = ggplot(df) + 
geom_point(aes(x, y), alpha = .1, size = 5, fill = 'black', colour = 'red') +                                        
  theme_bw() 
p

enter image description here

Yet the point does have a boundary line. The line's width can be changed with stroke; as follows:

p = ggplot(df) + 
geom_point(aes(x, y), stroke = 2, alpha = .1, size = 5, fill = 'black', colour = 'red') +                                        
  theme_bw() 
p

enter image description here

Unfortunately, setting stroke to zero will not remove the boundary line; it seems there is a lower limit.

To remove the boundary line, use one of the shapes that has a border that can be manipulated; for instance, shape = 21. Set its "fill" to red and its "colour" to transparent.

p = ggplot(df) + 
geom_point(aes(x, y), shape = 21, alpha = .1, size = 5, fill = 'red', colour = 'transparent') +                                      
  theme_bw() 
p

enter image description here

查看更多
登录 后发表回答