4

I want to set a ggplot's max y value while keeping the default min y but I need to use coord_cartesian as I want to zoom in, not exclude any data from calculations.

I'm surprised that I couldn't find the answer to this. There are many questions answering how to expand limits to a certain value or set only one limit using scale_._.. but I can't find any using coord_cartesian to set only one limit

For example; using the example plot from ?coord_cartesian:

library(ggplot2)
p <- ggplot(mtcars, aes(disp, wt)) +
  geom_point() +
  geom_smooth()

I want the equivalent of

p + coord_cartesian(ylim = c(1.5, 4))

without having to set 1.5 as the min.

If I could use scale_y_continuous I could use

p + scale_y_continuous(limits = c(NA, 4))

but this limits all data contributing to my smooth to values less than 4 which I don't want, and the equivalent p + coord_cartesian(ylim = c(NA, 4)) returns an error.

Is there an easy replacement?

I could also come up with a calculation based on the data of the min of the plot, but this could become complicated if I am faceting the plot or similar.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Sarah
  • 3,022
  • 1
  • 19
  • 40
  • This is an open issue on ggplot2's GH page ([link](https://github.com/tidyverse/ggplot2/issues/2907)), with a proposed fix for `ggplot2:::scale_range` that addresses the issue. – Z.Lin Apr 23 '19 at 05:27
  • Ah, okay, I guess i'll just have to wait and use work-arounds for the present – Sarah Apr 23 '19 at 05:35

0 Answers0