hrbrmstr / ggalt

:earth_americas: Extra Coordinate Systems, Geoms, Statistical Transformations & Scales for 'ggplot2'
https://cran.r-project.org/web/packages/ggalt/vignettes/ggalt_examples.html
Other
661 stars 99 forks source link

`ash1()` never sees `m` and `kopt` #87

Open fkohrt opened 6 months ago

fkohrt commented 6 months ago

The following code should produce (nearly¹) overlapping histograms:

faithful |>
  ggplot2::ggplot(ggplot2::aes(x = eruptions)) +
  ggplot2::stat_bin(
    closed = "right",
    breaks = breaks,
    mapping = ggplot2::aes(y = ggplot2::after_stat(density)),
    fill = NA,
    color = "blue"
  ) +
  ggalt::stat_ash(
    nbin = 18,
    ab = range(breaks),
    m = 1,
    geom = "bar",
    fill = NA,
    color = "red"
  )

...but it doesn't:

The reason is that ash::ash1() is never provided with m (or kopt), but is rather always called with the default m = 5:

https://github.com/hrbrmstr/ggalt/blob/30dc0dade4d0b8d78eddea7409a07f86dbbafa76/R/geom_ash.r#L115-L127


¹ In theory, the histograms should align perfectly. In practice, ash::bin1() differs from graphics::hist(), e.g. compare ash::bin1(faithful$eruptions, ab = c(1.6, 5.2), nbin = 18)$nc and hist(faithful$eruptions, breaks = 18, right = TRUE, plot = FALSE)$counts.