-2

EDIT: clarifying per @Gregor's comment, I would like to set bounds in the context of stat_summary - 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 scale_continuous.

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 scale_continuous 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 oob=censor.

For the life of me, I can find no other way to specify breaks than with scale_([xy]_)?continuous functions. [xy]lim and coord_cartesian 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.)

Dav Clark
  • 1,430
  • 1
  • 13
  • 26
  • I'm not sure what all your talk about zooming has to do with anything. Is the issue that you would like to (a) zoom in, (b) set breaks and (c) not omit data that is out of bounds? – Gregor Thomas Sep 01 '16 at 22:05
  • Also, do you think you could make a [reproducible example](http://stackoverflow.com/q/5963269/903061)? Your current code doesn't even have a data frame, but from the column names it doesn't look like built-in data - at last not any that I recognize. – Gregor Thomas Sep 01 '16 at 22:06
  • Why do you need to set specific breaks? – Roland Sep 01 '16 at 22:07
  • @Gregor I think the problem is simple enough that someone who knows an answer won't need a fully reproducible example. I have eliminated the mention of zooming from my text, and further simplified the code. – Dav Clark Sep 01 '16 at 22:16
  • @Roland, I am trying to reproduce a figure produced by SPSS in my lab. It has particular bounds. – Dav Clark Sep 01 '16 at 22:16
  • I've made a guess at an answer, but I'm still confused about your problem. The only actual question you have is whether it is warranted to mention in the vignettes that the `scale` functions, such as `scale_y_continuous`, are the way to set the breaks on the scale... – Gregor Thomas Sep 01 '16 at 22:19

1 Answers1

3

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.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • And indeed - you proved my suspicion was correct! But, now that I see your example, I grok the value of the reproducible example a bit more... – Dav Clark Sep 01 '16 at 22:21