exaexa / scattermore

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

Point shape #7

Closed bonifazi closed 1 year ago

bonifazi commented 3 years ago

Hi, I really like this package and its integration with ggplot. How can the shape be controlled? Within the geom_scattermore 'shape=1' is ignored. Thanks

exaexa commented 3 years ago

Hi!

Since scattermore uses its own rasterization that can't easily import R shapes, the point shape is actually unsupported and ignored. The only shape we know now is the antialiased circle with some defined pixel size.

I'm not planning to add support for this, neither for ggplot nor Rbase shapes -- at least not until scattermore internals get totally reorganized, similar to what I did with GigaScatter.jl. The 2 problems that prevent reasonable implementation now are:

I can see a usecase for this, for highlighting "important outliers" outside of the dense area in the scatterplot. In that case I suggest to use an additional geom_point that plots just the (correctly shaped) outliers above of the scattermore layer. Would that work for you?

bonifazi commented 3 years ago

Thanks for the quick and clear reply. Indeed, I didn't mention the reason behind my question. I was indeed trying to highlight some points overlapping for x-y coordinates as "outliers". In base plot or ggplot2, I used a different colour for these outliers, but as there are many points, I also used an alpha for transparency and/or use shape=1 (the empty circle) to make them more visible. Your suggestion of making a second geom_point layer (with the outliers on top) didn't occur to me. You are right, that would do the trick in my case, thank you!

exaexa commented 3 years ago

ok cool, thanks for confirming! Could you please eventually share a minimal demo of the result? I'd pin this issue for others who'd need to solve the same problem. :]

bonifazi commented 3 years ago

Here. It is just a quick example (not really accurate), but I think the idea of using the two layers is shown.

First, create some data. The data are similar to the examples of scattermore's Readme page. Here we just add a third column with "red" or "black" to colour the points. The red points are outliers we want to show on top.

d <- cbind(rnorm(1e5),runif(1e5))
d <- cbind(rnorm(1e5),runif(1e5))
test_data <- d %>% as.data.frame %>% 
  mutate(color=case_when(
    (V1 < 0.1 & V1 > -0.1)~ "red", # define as outliers points between -0.1 and +0.1 (you can use your own criteria), these get color "red"
    TRUE ~ "black" # the rest gets color "black"
  )) 

table(test_data$color)
black   red 
92169  7831

Then we plot. First, plot the black dots in a first layer using scattermore::geom_scattermost(). Second, we plot a second layer on top of the first one with the red dots (outliers) using ggplot2::geom_point()

make outliers on top

ggsave('scattermore.png', units='in', width=3, height=3,
       ggplot() +
         geom_scattermost(  # first layer
           test_data[test_data$color=="black", c(1:2)], # first layer: select only black dots
           col="black",
           pointsize=1,
           pixels=c(700,700)) +
         geom_point(  # second layer
           data=test_data[test_data$color=="red",c(1:2)], # second layer: select only red dots
           aes(V1, V2), colour="red", size=0.05
           )+
         ggtitle("geom_scattermost"))

Output plot with red dots on top:

image

exaexa commented 3 years ago

OK perfect, thanks!

exaexa commented 2 years ago

This can be supported in scattermore v2 using some kind of small point masks (thanks @gitdemont for the idea, cf. here https://github.com/gitdemont/IFC/blob/master/inst/include/plot.hpp ). Reopening. :]

exaexa commented 2 years ago

@bonifazi the new version of scattermore (now in master) expands the points with a postprocessing "kernel", which you can use to plot any shape you like. We'll eventually make a small tutorial on how to work with that.

bonifazi commented 2 years ago

@exaexa that is great! Thanks for the implementation!

exaexa commented 2 years ago

anyway the new API is a bit more complicated because it also does 345978 other things, please hold on until a comprehensive documentation materializes :]

exaexa commented 2 years ago

(Added the docs label because now it's really just about writing a vignette that shows this :D )

exaexa commented 1 year ago

OK we've got support together with the documentation here: https://exaexa.github.io/scattermore/reference/apply_kernel_rgbwt.html If you want to have outliers outstanding, I wholeheartedly recommend to plot them in a separate layer and then merge the layers using RGBA (as opposed to RGBWT) merging: https://exaexa.github.io/scattermore/reference/blend_rgba_float.html (thx @teri934 for implementing)