96

Is it possible to only set the lower bound of a limit for continuous scale? I want to make all my plots 0 based without needing to specify the upper limit bound.

e.g.

+ scale_y_continuous(minlim=0)
Mark
  • 3,177
  • 4
  • 26
  • 37

5 Answers5

141

You can use expand_limits

ggplot(mtcars, aes(wt, mpg)) + geom_point() + expand_limits(y=0)

Here is a comparison of the two:

  • without expand_limits

  • with expand_limits

As of version 1.0.0 of ggplot2, you can specify only one limit and have the other be as it would be normally determined by setting that second limit to NA. This approach will allow for both expansion and truncation of the axis range.

ggplot(mtcars, aes(wt, mpg)) + geom_point() +
  scale_y_continuous(limits = c(0, NA))

specifying it via ylim(c(0, NA)) gives an identical figure.

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
11

How about using aes(ymin=0), as in:

ggplot(mtcars, aes(wt, mpg)) + geom_point() + aes(ymin=0)
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 7
    This overrides the ymin for geom_errorbar, etc.; expand_limits() seems safer. – Mark Jul 23 '12 at 21:58
  • This is not universal, as for a ``geom_density`` it gives me ``Error: stat_bin() must not be used with a y aesthetic`` or when placed inside the ``geom_density``, ``Warning: Ignoring unknown aesthetics: ymin`` (the latter being fully expected) – PatrickT Oct 02 '17 at 20:41
  • `aes(ymin=o)` does not set the minimum of the vertical axis. This is not an answer to the question. – randy Jun 17 '23 at 21:04
9

You can also try the following code which will give you the min y-axis at zero and also without the extra gap between x-axis and min y value.

scale_y_continuous(limits = c(0, NA), expand = c(0,0))
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
0

If you'd like to keep the upper extent of the scale "unchanged" from what ggplot would have calculated by default, AND eliminate the padding on the lower bound so the plot area starts at exactly 0, as of ggplot2 v3.0.0+ you can specify separate expansion values for the upper and lower limit of the scales.

scale_y_continuous(limits = c(0, NA), expand = expansion(mult=c(0, 0.05)))

see the description of the limits and expand arguments in the help for scale_continuous(),

Use the convenience function expansion() to generate the values for the expand argument. The defaults are to expand the scale by 5% on each side for continuous variables, and by 0.6 units on each side for discrete variables.

we use the mult argument to expansion() to calculate the 5% expansion.

Examples

ggplot(mtcars, aes(wt, mpg)) + geom_point() +
  labs(subtitle="default")

default plot

ggplot(mtcars, aes(wt, mpg)) + geom_point() +
  scale_y_continuous(limits=c(0, NA))+
  labs(subtitle="specifying limits alone leaves default 5% padding on both ends")

limits-alone

ggplot(mtcars, aes(wt, mpg)) + geom_point() +
  scale_y_continuous(limits=c(0, NA), expand=expansion(mult=c(0, 0.05))) +
  labs(subtitle = "limits and an unequal expansion gets what we're after")

limits-and-expansion

mac
  • 3,137
  • 1
  • 28
  • 42
-1

I don't think you can do this directly. But as a work-around, you can mimic the way that ggplot2 determines the upper limit:

scale_y_continuous(limits=c(0, max(mydata$y) * 1.1))
bdemarest
  • 14,397
  • 3
  • 53
  • 56