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

Consistent y-axis height for stat_dots #26

Open
bwiernik opened this issue Aug 16, 2020 · 3 comments
Open

Consistent y-axis height for stat_dots #26

bwiernik opened this issue Aug 16, 2020 · 3 comments
Labels
enhancement New feature or request rewrite requires re-architecting some internals

Comments

@bwiernik
Copy link
Contributor

I love the stat_dots() slab element, especially the quantile version. But it seems to have inherited the major limitation of geom_dotplot() that the linear scale of the y-axis is meaningless.

I'm interested in combining a dots slab with a curve (e.g., to show the observed group distributions against reference normal distributions), but I'm struggling with a way to get consistent y-axis heights for the dots stacks and the density curve, especially across image sizes. See the reprex below.

Is there any way that different slab elements could be forced to use the same linear y-axis scale as the dots slab?

library(dplyr)
library(tidyr)
library(ggdist)
library(ggplot2)
library(broom)
library(modelr)

theme_set(theme_ggdist())

set.seed(5)
n = 10
n_condition = 5
ABC =
  tibble(
    condition = rep(c("A","B","C","D","E"), n),
    response = rnorm(n * 5, c(0,1,2,1,-1), 0.5)
  )
m_ABC = lm(response ~ condition, data = ABC)

ABC %>%
    data_grid(condition) %>%
    augment(m_ABC, newdata = ., se_fit = TRUE) %>%
    ggplot(aes(y = condition)) +
    stat_dist_halfeye(
        aes(dist = distributional::dist_normal(.fitted, sigma(m_ABC)))
    ) +
    # we'll add the data back in too (scale = .5 above adjusts the halfeye height so
    # that the data fit in as well)
    stat_dots(quantiles = 100, aes(x = response, y = condition), data = ABC, color = "blue")
@mjskay
Copy link
Owner

mjskay commented Aug 17, 2020

This is a really good question to which I would love to be able to answer "yes" 🙂. I like the idea of combining densities with dotplots and dearly wish it was easier... Unfortunately at the moment there's no easy fix. The problem is the automatic bin adjustment on the dotplots needs to be calculated at display time (because it depends on chart size), which can only be done by making the dotplots into their own grob objects (the underlying geometry objects used by {grid}). To coordinate across the dotplots and the densities they would probably all need to be in the same grob object (that, or I'd have to come up with some bizarre hack to coordinate across multiple grobs in a way {grid} is not really designed to do). That would likely require them all to be created in the same geom, or I'd have to add a way to coordinate across geoms as well. So any solution would require non-trivial re-architecting of the internal code and careful thought about what the API should look like. I'm not against this in principle (in fact I've already done this kind of thing several times in tidybayes) but it's definitely not a quick fix, and I'm not even 100% sure it's possible. I'm also not sure exactly what the right relationship between the dotplot scaling and density scaling should be. Happy to take suggestions.

So this isn't a "no", because it is a use case I would love to support! It just is not likely to be anytime soon 🙂.

Sidebar: in your example since you only have a few samples per group I would use regular old dotplots, not quantile dotplots. If you drop the quantiles argument from stat_dots the output looks more reasonable to me.

@bwiernik
Copy link
Contributor Author

Sidebar: in your example since you only have a few samples per group I would use regular old dotplots, not quantile dotplots. If you drop the quantiles argument from stat_dots the output looks more reasonable to me.

Yeah, I looked at that, but even then it seems quite dependent on the size of the output; it's hard to see how reasonable

I'm also not sure exactly what the right relationship between the dotplot scaling and density scaling should be. Happy to take suggestions.

Yeah, it's tough when the y-axis scaling in a dotplot is pretty arbitrary. I wonder if something like drawing the sample density curve, then scaling the dots based on that curve might work? Or perhaps scaling the y-axis based on a histogram with the same binwidth as the dotplot? An alternative might be to adjust the .width or scaling of the other geom, e.g., make a histogram that lines up with the dotplot, then use that y-axis scaling for the density curve?

@mjskay mjskay added enhancement New feature or request rewrite requires re-architecting some internals labels Nov 22, 2020
@mjskay
Copy link
Owner

mjskay commented May 11, 2023

FYI for getting KDE shape to match a dotplot I might have a slightly better solution now: #178 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rewrite requires re-architecting some internals
Projects
None yet
Development

No branches or pull requests

2 participants