Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalized density for facetted plots #1

Closed
seasmith opened this issue Sep 3, 2019 · 4 comments
Closed

Normalized density for facetted plots #1

seasmith opened this issue Sep 3, 2019 · 4 comments
Labels
enhancement New feature or request

Comments

@seasmith
Copy link

seasmith commented Sep 3, 2019

Would it make sense to create a computed statistic to show the normalized/relative number of neighbors per group to the max nearest neighbors?

# i.e.
data$r_neighbors <- data$n_neighbors / max(data$n_neighbors)
@LKremer LKremer added the enhancement New feature or request label Sep 4, 2019
@LKremer
Copy link
Owner

LKremer commented Sep 4, 2019

Yes it would make sense in some cases! I wouldn't want to make this the default behavior because I think raw neighbor counts are a bit more intuitive than relative ones, but for facetted plots I see how it can be useful. Maybe something like geom_pointdensity(relative=TRUE) would be worthwhile?

@seasmith
Copy link
Author

seasmith commented Sep 6, 2019

Returning a computed stat would bring the function's behavior inline with other ggplot2 functions (i.e. stat_density_2d returns both density, ndensity, level, and nlevel).

# Example
library(ggplot2)
library(ggpointdensity)

ggplot(diamonds, aes(carat, price)) +
  geom_pointdensity(aes(color = stat(r_neighbors)))

I feel density and ndensity are more inline with ggplot2 and would make the function more extendible (i.e. if the function accepted something like method = "kde2d" for 2d kernel-density or method = "bkde2d" for 2d binned kernel-density).

# Example

# Default method would be 'nn'
ggplot(diamonds, aes(carat, price)) +
  geom_pointdensity(aes(color = stat(ndensity)), method = "nn")

# kernel-density
ggplot(diamonds, aes(carat, price)) +
  geom_pointdensity(aes(color = stat(ndensity)), method = "bkde2d")

# binned kernel-density
ggplot(diamonds, aes(carat, price)) +
  geom_pointdensity(aes(color = stat(ndensity)), method = "bkde2d")

@LKremer
Copy link
Owner

LKremer commented Sep 6, 2019

Returning a computed stat would bring the function's behavior inline with other ggplot2 functions (i.e. stat_density_2d returns both density, ndensity, level, and nlevel).

This is already the case. stat_pointdensity computes a stat called n_neighbors.
I just realized you can even use this stat to plot the density as you originally proposed:

ggplot(dat, aes(x = x, y = y, color = stat(n_neighbors) / max(n_neighbors))) +
    geom_pointdensity() +
    scale_color_viridis()

I could tweak the stat_pointdensity to return both n_neighbors and the density for convenience.

Regarding your last suggestion with method = "something", I'm experimenting with something like this at the moment. Mostly to test out different algorithms to find an efficient one that can handle many points (issue #2).

@LKremer
Copy link
Owner

LKremer commented Feb 7, 2020

This was implemented in @bjreisman's recent pull request #8 , so I'm closing.

@LKremer LKremer closed this as completed Feb 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants