set only lower bound of a limit for ggplot

RGgplot2

R Problem Overview


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)

R Solutions


Solution 1 - R

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

![][1]

  • with expand_limits

![][2]

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

![][3]

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

[1]: http://i.stack.imgur.com/EWw6I.png "ggplot(mtcars, aes(wt, mpg)) + geom_point()" [2]: http://i.stack.imgur.com/KiuV6.png "ggplot(mtcars, aes(wt, mpg)) + geom_point() + expand_limits(y=0)" [3]: http://i.stack.imgur.com/YttIu.png "ggplot(mtcars, aes(wt, mpg)) + geom_point() + scale_y_continuous(limits = c(0, NA))"

Solution 2 - R

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

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

Solution 3 - R

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

Solution 4 - R

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarkView Question on Stackoverflow
Solution 1 - RBrian DiggsView Answer on Stackoverflow
Solution 2 - RJosh O'BrienView Answer on Stackoverflow
Solution 3 - RWANNISA RITMAHANView Answer on Stackoverflow
Solution 4 - RbdemarestView Answer on Stackoverflow