Dav Clark - 9 months ago 94

R Question

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

`stat_summary`

`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`

`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`

`[xy]lim`

`coord_cartesian`

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

Answer

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)
```

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.

Source (Stackoverflow)