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

Scaling of quantile dotplots to a target area #178

Open
TeemuSailynoja opened this issue May 10, 2023 · 1 comment
Open

Scaling of quantile dotplots to a target area #178

TeemuSailynoja opened this issue May 10, 2023 · 1 comment

Comments

@TeemuSailynoja
Copy link

Problem

When combining quantile dotplots and density plots, there isn't a way to automatically make the dotplot scale match that of a density plot.

image

A workaround could be to compute a density estimate and set scale = 1 and height to the maximum density value of the KDE.
image

But this approach has issues, when the sample is not as smoothly distributed:
image

Question / solution

Would it be possible to make the area covered by the quantile dot plot sum to one?
Perhaps by thinking of the dots as squares and using quantiles x binwidth × dot_height = 1 as the target for the dot arrangement?

@mjskay
Copy link
Owner

mjskay commented May 11, 2023

Hmm I'm not sure this would solve the problem, as I think the two shapes lining up well also depends on the binwidth being about twice the bandwidth of the density estimator (as in your example with the spike). So you can kind of do it by setting the bandwidth and binwidth manually; e.g.:

x = qnorm(ppoints(100))
bw = 0.1

ggplot(data.frame(x), aes(x)) + 
  geom_dots(aes(x), binwidth = c(2 * bw, Inf), overflow = "compress") +
  stat_slab(fill = NA, color = "black", density = density_unbounded(bandwidth = bw))

image

Or:

x = c(qnorm(ppoints(50)), rep(0, 10))
bw = 0.1

ggplot(data.frame(x), aes(x)) + 
  geom_dots(aes(x), binwidth = c(2 * bw, Inf), overflow = "compress") +
  stat_slab(fill = NA, color = "black", density = density_unbounded(bandwidth = bw))

image
image
image

Doesn't always work perfectly but I'm not sure there is a solution that will. This uses a combination of setting a lower bound on the binwidth to be twice the bandwidth of the KDE (binwidth = c(2 * bw, Inf)) and telling it to compress the spacing between dots as necessary when the layout would otherwise exceed the bounds of the plot to keep that minimum binwidth (overflow = "compress").

It unfortunately does require specifying the binwidth manually to match the bandwidth of the KDE, though I'm not sure of a way around that...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants