Dav Clark Dav Clark - 5 months ago 49
R Question

Set breaks / labels with stat_summary in GGplot2

EDIT: clarifying per @Gregor's comment, I would like to set bounds in the context of

- in this case actually zooming out from the default. Only pre-mean data falls out side my desired bounds. Then, I'd like to set breaks, which seems to require the use of

I've looked around for a while, and at least some mailing list posts describe "problem(s) that new users sometimes have" with setting scales.

However, while I am no longer confused at that level about
censoring out-of-bounds data, it seems I am reduced to the following awkward code to specify breaks if using stat_summary:

ggplot(data, aes(x=trial, linetype=PrimaryDx, y=mTT, color=hand)) +
stat_summary(fun.y = mean, geom = "line") +
scale_y_continuous(limits = c(1,2), breaks = seq(1, 2, 0.2),
oob=function(x, r) x) ## This seems silly

Here, scale_y_continuous of course will censor out-of-bounds data by default. I supply a simple identity function in place of

For the life of me, I can find no other way to specify breaks than with
both only set limits.

Is the above is the only way to do it? If so, then maybe that at least warrants a place in the vignettes? (or if there is a better way, then maybe that should go in the vignettes.)


You can use coord_cartesian to zoom without omitting out-of-bounds data and still set breaks using scale_y_continuous. Adding scale_y_continuous to a plot doesn't mean that you have to set the limits inside scale_y_continuous.

Demonstrating with mtcars:

p = ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
    geom_boxplot() +
    scale_y_continuous(breaks = c(15.2, 19.7, 26))

gridExtra::grid.arrange(p, p + coord_cartesian(ylim = c(15, 28)), ncol = 2)

enter image description here

The above plot demonstrates this. I set the y breaks at the medians so that it is clear that the median in the boxplot summary remains unchanged after the zoom.