Home > database >  extreme alpha scaling unexpectedly leads to completely transparent geoms
extreme alpha scaling unexpectedly leads to completely transparent geoms

Time:03-25

I want to use geom_point to plot a lot of points with extremely high transparency, i.e. alpha values close to 0.

The issue is that there appears to be a 'breaking point' at which no points are being plotted at all anymore. In the code below I'd expect the plot in the bottom right to still show points but there is nothing being plotted.

What is going on?

library(tidyverse)
library(patchwork)
someData <- data.frame(x = rnorm(n = 100000),
                       y = rnorm(n = 100000)) %>%
  mutate(alp = 1)
p1 <- ggplot(someData)  
  geom_point(aes(x = x, y = y, alpha = alp))  
  scale_alpha_continuous(range = c(0, 0.5))
p2 <- ggplot(someData)  
  geom_point(aes(x = x, y = y, alpha = alp))  
  scale_alpha_continuous(range = c(0, 0.05))
p3 <- ggplot(someData)  
  geom_point(aes(x = x, y = y, alpha = alp))  
  scale_alpha_continuous(range = c(0, 0.005))
p4 <- ggplot(someData)  
  geom_point(aes(x = x, y = y, alpha = alp))  
  scale_alpha_continuous(range = c(0, 0.0025))

print((p1 p2)/(p3 p4))

CodePudding user response:

The answer here is that there are only 256 alpha values available (0:255), so alpha values of less than 1/255 are treated as 0.

To show this as a minimal example:

p1 <- ggplot(someData)  
  geom_point(aes(x = x, y = y, alpha = alp))  
  scale_alpha_continuous(range = c(0, 1/254.99))
p2 <- ggplot(someData)  
  geom_point(aes(x = x, y = y, alpha = alp))  
  scale_alpha_continuous(range = c(0, 1/255.01))

print(p1   p2)

enter image description here

For data this dense, you may be better sticking to geom_density2d_filled

enter image description here

  • Related