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

fill not quite right in some gf_dist plots #113

Closed
homerhanumat opened this issue Sep 26, 2018 · 6 comments
Closed

fill not quite right in some gf_dist plots #113

homerhanumat opened this issue Sep 26, 2018 · 6 comments

Comments

@homerhanumat
Copy link

This works well enough:

x <- seq(0.25, 0.75, by = 0.01)
y <- dbeta(x, 2, 4)
df <- data.frame(x, y)
gf_dist("beta", params = list(shape1 = 2, shape2 = 4)) +
  geom_area(data = df,
            mapping = aes(x = x, y = y),
            fill = "burlywood")

geom_fill

But this is visibly off:

gf_dist("beta", params = list(shape1 = 2, shape2 = 4),
            fill = ~(x >= 0.25 & x <= 0.75),
            geom = "area")

paramfill

@rpruim
Copy link

rpruim commented Sep 26, 2018

Actually, the second plot is doing just what ggplot2 thinks you are asking for. So the question is whether we should add something to gf_dist() to make the kind of plot you want more easily.

Here's what's going on in the example above: There are only two groups (TRUE and FALSE), and geom_area() doesn't treat the two tail pieces separately unless you make them different groups (you could color them the same with custom scales). So that bit of extra "red" is part of a larger shaded region, most of which is covered by the central piece. If we set alpha lower we can see this more clearly:

library(ggformula)
theme_set(theme_bw())
gf_dist("beta", params = list(shape1 = 2, shape2 = 4),
        fill = ~(x >= 0.25 & x <= 0.75),
        geom = "area", alpha = 0.3) 

One solution is to use cut() to create three groups:

gf_dist("beta", params = list(shape1 = 2, shape2 = 4),
        fill = ~cut(x, c(-Inf, 0.25, 0.75, Inf)),
        geom = "area", alpha = 0.3) 

Created on 2018-09-26 by the reprex package (v0.2.0).

@rpruim
Copy link

rpruim commented Sep 26, 2018

One way to make things easier might be to add a breaks argument and have the stat create a variable that could be used for fill. You'd still need a custom scale to get the tails to be the same color, however.

I'll have to think about whether (and how best) to implement something like this.

@rpruim
Copy link

rpruim commented Sep 26, 2018

Side note: you can avoid the use of list() and layer things without reverting to + and direct calls to geoms.

library(ggformula)
theme_set(theme_bw())
gf_dist("beta", shape1 = 2, shape2 = 4,
        fill = ~cut(x, c(-Inf, 0.25, 0.75, Inf)),
        geom = "area", alpha = 0.5)  %>%
  gf_dist("beta", shape1 = 2, shape2 = 4)

Created on 2018-09-26 by the reprex package (v0.2.0).

@homerhanumat
Copy link
Author

Thanks, no big hurry, as my students aren't making these graphs yet. reprex seems quite handy!

@rpruim
Copy link

rpruim commented Sep 27, 2018

Alternatively, you could do it this way. As bonus you get nice labeling. If you want to further modify the plot, be sure to use return = "plot". The default returns the probabilities as a vector, just like pbeta(). This is how I have students build these plots.

library(mosaic)
theme_set(theme_bw())
xpbeta(c(0.25, 0.75), shape1 = 2, shape2 = 4)

#> [1] 0.3671875 0.9843750

Created on 2018-09-27 by the reprex package (v0.2.0).

@rpruim
Copy link

rpruim commented Jan 11, 2019

I'm going to close this since the behavior is as "expected", ie, just like ggplot2. But I'll open a new issue about possibly adding some functionality to gf_dist() in the future.

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