mjskay / tidybayes

Bayesian analysis + tidy data + geoms (R package)
http://mjskay.github.io/tidybayes
GNU General Public License v3.0
718 stars 59 forks source link

stat_lineribbon introduces non-linear artifacts when using axis limits #169

Closed corriebar closed 5 years ago

corriebar commented 5 years ago

stat_lineribbon introduces non-linear artifacts (for linear models) when zooming in on certain parts of the plot:

tibble(x = 1:10) %>%
  group_by_all() %>%
  do(tibble(y = rnorm(100, .$x))) %>%
  ggplot(aes(x = x, y = y)) +
  stat_lineribbon() +
  scale_fill_brewer() +
  ylim(0, 8)

image

For geom_lineribbon some of the outer ribbons are lost in the plot, which makes it more obvious that the plot is faulty:

tibble(x = 1:10) %>%
  group_by_all() %>%
  do(tibble(y = rnorm(100, .$x))) %>%
  median_qi(.width = c(.5, .8, .95)) %>%
  ggplot(aes(x = x, y = y)) +
  geom_lineribbon() +
  scale_fill_brewer() +
  ylim(0, 8)

image

mjskay commented 5 years ago

I think this is a general issue with using ylim() to limit axes / to zoom in, not an issue with lineribbon: ylim() removes data outside of its bounds instead of drawing the plot and clipping it, which leads to weird behavior for most geoms.

If you replace ylim(0, 8) with coord_cartesian(ylim = c(0, 8)) do you get the expected behavior?

See also this stackoverflow post: https://stackoverflow.com/questions/25685185/limit-ggplot2-axes-without-removing-data-outside-limits-zoom

corriebar commented 5 years ago

Yes, you're right, coord_cartesian(ylim=c(0,8)) works as expected.

mjskay commented 5 years ago

Great!