Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand ggplot y axis limits to include maximum value

Tags:

r

ggplot2

Often in plots the Y axis value label is chopped off below the max value being plotted.

For example:

library(tidyverse)
mtcars %>% ggplot(aes(x=mpg, y = hp))+geom_point()

I know of scale_y_continous - but I can't figure out a smart way to do this. Maybe I'm just overthinking things. I don't wish to mess up the 'smart' breaks that are generated automatically.

I might try to go about this manually...

  mtcars  %>% ggplot(aes(x=mpg, y=hp, color=as.factor(carb)))+geom_point()  + scale_y_continuous(limits = c(0,375))

enter image description here

But this doesn't work like I mentioned above because of the 'smart breaks'. Is there anyway for me to extend the default break interval to 1 more, so that in this case it would be 400? Of course I would want this to be flexible for whatever dataset I am working with.

like image 814
runningbirds Avatar asked Nov 29 '18 05:11

runningbirds


People also ask

How do I increase y axis limit in R?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

How do you set limits on the y axis?

ylim( limits ) sets the y-axis limits for the current axes or chart. Specify limits as a two-element vector of the form [ymin ymax] , where ymax is greater than ymin .

How do you change the scale of the axis in Ggplot?

Use scale_xx() functions It is also possible to use the functions scale_x_continuous() and scale_y_continuous() to change x and y axis limits, respectively.


2 Answers

You can use expand_limits() to increase the maximum y-axis value. You can also ensure that the maximum y-axis value is rounded up to the next highest value on the scale of the data, e.g., next highest tens value, next highest hundreds value, etc., depending on the whether the highest value in the data is within the tens, hundreds, etc.

For example, the function below finds the base 10 log of the maximum y value and rounds it down. This gives us the base ten scale of the maximum y value (e.g., tens, hundreds, thousands, etc.). It then rounds the maximum y-axis value up to the nearest ten, hundred, etc., that is higher than the maximum y value.

expandy = function(vec, ymin=NULL) {

  max.val = max(vec, na.rm=TRUE)
  min.log = floor(log10(max.val))

  expand_limits(y=c(ymin, ceiling(max.val/10^min.log)*10^min.log))
}

p = mtcars %>% ggplot(aes(x=mpg, y = hp)) +
  geom_point()

p + expandy(mtcars$hp)

p + expandy(mtcars$hp, 0)

Or, to make things a bit easier, you could set up the function so that the y-range data is collected directly from the plot:

library(gridExtra)

expandy = function(plot, ymin=0) {

  max.y = max(layer_data(plot)$y, na.rm=TRUE)
  min.log = floor(log10(max.y))

  expand_limits(y=c(ymin, ceiling(max.y/10^min.log)*10^min.log))
}

p = mtcars %>% ggplot(aes(x=mpg, y = hp)) +
  geom_point()

grid.arrange(p, p + expandy(p), ncol=2)

enter image description here

p = iris %>% ggplot(aes(x=Sepal.Width, y=Petal.Width)) +
  geom_point()

grid.arrange(p, p + expandy(p), ncol=2)

enter image description here

like image 115
eipi10 Avatar answered Oct 16 '22 22:10

eipi10


Choosing a step for breaking the y axis you can use the ceiling() function

library(gridExtra)

p1 <- mtcars %>% ggplot(aes(x=mpg, y = hp)) + geom_point() 

p2 <- p1 +
  scale_y_continuous(
    limits = c(0, ceiling(max(mtcars$hp)/50)*50), 
    breaks = seq(0, ceiling(max(mtcars$hp)/50)*50, 50)
  ) 

p3 <- p1 + scale_y_continuous(
  limits = c(0, ceiling(max(mtcars$hp)/100)*100), 
  breaks = seq(0, ceiling(max(mtcars$hp)/100)*100, 100)
) 

grid.arrange(p1, p2, p3, ncol=3)

For the p2 the ste is 50 while for p3 the step is 100

enter image description here

like image 1
Tiziano Avatar answered Oct 16 '22 22:10

Tiziano