ProjectMOSAIC / ggformula

Provides a formula interface to 'ggplot2' graphics.
Other
39 stars 11 forks source link

gf_dist not working as we predict... #147

Closed jiyunson closed 2 years ago

jiyunson commented 3 years ago

As always, we're super appreciative of all the features of ggformula (and the MOSAIC approach in general)! We're always evangelizing about this package to others who have been teaching R without it. We're huge fans!

We haven't been able to get gf_dist() to work as we expect though... for example, this bit of code works as expected:

M <- 0
SD <- .026
gf_dist("norm", mean=M, sd=SD, xlim=c(-.2,.2)) %>%
gf_vline(xintercept=M + 2*SD, color="blue") %>%
gf_vline(xintercept=M - 2*SD, color="blue")

But, if we change mean to -.1 it doesn't work; or if we change xlim to (-.3,.3). Any hints as to how we can understand how this function works better?

rpruim commented 3 years ago

This might be a bug in xlim. (Seems like a strange bug, so I'm curious to see what I messed up.)

In the meantime, you could try using gf_lims(x = c( ... )). That won't be quite the same thing, but in many situations it might be just what you need.

rpruim commented 3 years ago

Finally getting around to looking at this. The problem arrises when Inf or -Inf shows up as an endpoint for the range. In that case seq() can't create a uniform sequence over the range.

This should be fixable.

rpruim commented 3 years ago

I have a POC working:

suppressPackageStartupMessages(library(ggformula))
theme_set(theme_bw())
M <- 0
SD <- .026
gf_dist("norm", mean = M, sd = SD) %>%
  gf_vline(xintercept = ~(M + 2*SD), color = "blue") %>%
  gf_vline(xintercept = ~(M - 2*SD), color = "blue")

gf_dist("norm", mean = M, sd = SD, xlim = c(-0.3, 0.3)) %>%
  gf_vline(xintercept =  ~ (M + 2*SD), color = "blue") %>%
  gf_vline(xintercept =  ~ (M - 2*SD), color = "blue")

Created on 2021-06-21 by the reprex package (v0.3.0)

I expect this will be in the next release of the package.