ggplot2强制y轴从原点开始并浮点y轴上限 [英] ggplot2 Force y-axis to start at origin and float y-axis upper limit
问题描述
有没有一种简单的方法可以制作一个y轴的图形,该图形始于原点,并且很清楚显示你所有的数据?
这是我的问题:
set.seed(123)
my.data< - data.frame(x.var = rnorm(100,50),
y.var = rnorm(100,50,10))
##恼人,因为它不是从原点开始
ggplot(my.data,aes(x.var,y.var))+
geom_point()
##恼人,因为原点不在底部
ggplot(my.data,aes(x.var,y.var))+
geom_point()+
expand_limits(y = 0)
##恼人,因为点被切断
ggplot(my.data,aes(x.var,y.var))+
geom_point()+
scale_y_continuous(expand = c(0,0))+
expand_limits(y = 0)
问题强制原点在ggplot2(R)中从0开始的问题的最佳答案以
结尾您可能需要稍微调整一些内容以确保积分不会中断
为什么会发生这种情况?我可以手动调整坐标轴,但我不希望每次都这样做!
一些在网络上的家伙有一个解决方案,涉及到
#找到上限
#的当前ymax值(通过http://stackoverflow.com/questions/7705345/how-can
gy = ggplot_build(g)$ panel $ ranges [[1]] $ y.range [2] $ b $提取绘图 - 绘制坐标轴范围 - 对于ggplot2 - 对象#comment24184444_8167461) bg = g + ylim(0,gy)
#通过扩展x轴来处理溢出
g = g + scale_x_continuous(expand = c(0.1,0))
看起来很复杂,我觉得这是一个相对简单的想法。我错过了什么吗?
谢谢!
>
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://stackoverflow.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)))
这篇关于ggplot2强制y轴从原点开始并浮点y轴上限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!