exaexa / scattermore

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

Is it possible to define colors using hue_pal() from scales? #6

Closed SathiyaNManivannan closed 3 years ago

SathiyaNManivannan commented 3 years ago

Hi, I am trying to see if it would be possible to change the colors in Scattermost with hue_pal(). When using discrete data, where the number of colors required > 10, the colors seem to be recycled. I tried scale_color_manual(values = scales::hue_pal()(30)), but that seems to be ignored by Scattermost Also, the addition of a color guide for discrete scale values like scale_color_discrete() will be useful. Thank you!

exaexa commented 3 years ago

Hello!

The whole purpose of geom_scattermost is to circumvent much of the ggplot machinery (which is comparatively slow) to gain speed. In turn, ggplot cannot compute any point-related values. Your choices are either to compute the color scales manually, or use geom_scattermore.

This works for me for the continuous colors:

library(scattermore)
library(ggplot2)
d <- data.frame(x=rnorm(100), y=rnorm(100), val=runif(100))
ggplot(d, aes(x,y,color=val)) +
  geom_scattermore(pointsize=5, pixels=c(1024,1024)) + 
  scale_color_gradientn(colors=scales::hue_pal()(30))

2021-04-24-173749_671x673_scrot

...and this works for the discrete colors:

library(scattermore)
library(ggplot2)
d <- data.frame(x=rnorm(100), y=rnorm(100), val=factor(sample(5,100,replace=T)))
ggplot(d, aes(x,y,color=val)) +
  geom_scattermore(pointsize=5, pixels=c(1024,1024)) + 
  scale_color_brewer(palette='Spectral')

2021-04-24-174141_671x673_scrot

SathiyaNManivannan commented 3 years ago

Thank you for your kind and prompt response! Would it be possible to get a color guide or a legend for scattermoreplot() or geom_scattermore()? when using these plot appears without any way to assess color assignment to different points.

exaexa commented 3 years ago

Plotting with geom_scattermore() should have the color guide added automagically from ggplot, as above. For scattermoreplot() I'm not sure -- it is base R plotting. You might need to call legend() manually there, such as they do it on this link: http://www.sthda.com/english/wiki/add-legends-to-plots-in-r-software-the-easiest-way .

exaexa commented 3 years ago

Closing this to keep the repo tidy, feel free to reopen with any more questions/hints/issues :]