且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ggplot2强制y轴从原点开始并浮点y轴上限

更新时间:2023-11-26 23:49:22

>

  ggplot(my.data,aes(x.var,y.var))+ 
geom_point()+
scale_y_continuous(expand = c(0,0))+
expand_limits(y = c(0,1.05 * max(my.data $ y.var)))


After a bit of searching I am still not happy!

Is there a simple way to make a graph with a y-axis that starts at the origin and clearly shows all your data?

Here's my problem:

set.seed(123)
my.data<- data.frame(x.var = rnorm(100, 50),
                     y.var = rnorm(100, 50,10))


## Annoying because it doesn't start at origin
ggplot(my.data, aes(x.var, y.var))+
  geom_point()


## Annoying because origin is not at bottom
ggplot(my.data, aes(x.var, y.var))+
  geom_point()+
  expand_limits(y = 0)

## Annoying because point is cut off
ggplot(my.data, aes(x.var, y.var))+
  geom_point()+
  scale_y_continuous(expand = c(0,0))+
  expand_limits(y = 0)

The top answer for the question "Force the origin to start at 0 in ggplot2 (R)" ends with

"You may need to adjust things a little to make sure points are not getting cut off"

Why does this happen? I could manually adjust the axis but I don't want to have to do that every time!

Some dude on the internet has a solution that involves

#Find the current ymax value for upper bound
#(via http://***.com/questions/7705345/how-can-i-extract-plot-axes-ranges-for-a-ggplot2-object#comment24184444_8167461 )
gy=ggplot_build(g)$panel$ranges[[1]]$y.range[2]
g=g+ylim(0,gy)

#Handle the overflow by expanding the x-axis
g=g+scale_x_continuous(expand=c(0.1,0))

Which seems complicated for what I feel like is a relatively simple idea. Am I missing something?

Thank you!

Why not just:

ggplot(my.data, aes(x.var, y.var))+
    geom_point()+
    scale_y_continuous(expand = c(0,0))+
    expand_limits(y = c(0,1.05 * max(my.data$y.var)))