exaexa / scattermore

very fast scatterplots for R
https://exaexa.github.io/scattermore/
GNU General Public License v3.0
243 stars 7 forks source link

overlapping points are semi-transparent #25

Open igordot opened 4 months ago

igordot commented 4 months ago

I am having some issues with the alpha parameter.

This is the ggplot2 version:

ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point(size = 10, alpha = 1)

image

And the scattermore version:

ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_scattermore(pointsize = 15, alpha = 1, pixels = c(300, 300))

image

As you can see, when the points are overlapping, they are semi-transparent if they are different colors.

I am using scattermore 1.2 and ggplot2 3.5.1.

exaexa commented 4 months ago

Hello, this is in fact the correct behavior; scattermore defaults to a blend mode that avoids overplotting. You might notice the points are actually not transparent, it's just that the color is a weighted average of all colors of points that occupy that given pixel.

The difference here is that ggplot and base R use the usual "layered" RGBA blending, which "hides" previous points behind new ones (just as the blue one behind the green ones in your case). Scattermore uses RGBWT by default, which always blends all layers equally. It's certainly not very "nice" for a few huge points like in your example here, but it helps a lot for huge amounts of little points (which is the target use-case of scattermore). E.g., for single-cell data vis, this makes sure you don't accidentally miss a small cluster in your picture.

If you want to have some points hidden explicitly, the best way is to use the lower-level API (e.g. this and this) to create RGBA layers and blend them in your preferred order. We don't have dedicated "frontend" functionality for this because it's a completely different use-case.

Hope this explanation helps :] I'll leave this open for now; maybe I'll find a bit of time to add the low-level-API solution to the docs at some point.

igordot commented 4 months ago

Thank you for the clarification. Would you be able to provide an example of how to remove the blending?

I was just using big points to have a very clear example. For little points, I agree blending can be helpful, but not necessarily. For example, if you have red and blue points, purple areas are mixed and blending makes sense. If you have red, blue, and purple points, then purple areas can be red+blue, red+blue+purple, or just purple, so blending introduces ambiguity.