Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set only lower bound of a limit for ggplot

Tags:

r

ggplot2

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) 
like image 728
Mark Avatar asked Jun 26 '12 18:06

Mark


2 Answers

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.

like image 134
Brian Diggs Avatar answered Sep 20 '22 08:09

Brian Diggs


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

ggplot(mtcars, aes(wt, mpg)) + geom_point() + aes(ymin=0) 
like image 34
Josh O'Brien Avatar answered Sep 22 '22 08:09

Josh O'Brien