geom_smooth()-缩放y轴,从平滑中丢失数据 [英] geom_smooth() - and scaling the y axis, losing data from smoothing

查看:36
本文介绍了geom_smooth()-缩放y轴,从平滑中丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,此示例的功能不是很好,但确实突出了这一点.

Apologies that this example isn't that great but it does highlight the point.

mtcars$tran <- factor(mtcars$am, labels=c("Man","Aut"))
ggplot(mtcars, aes(x=hp, y= mpg, group=tran)) + geom_smooth(aes(colour=tran))
ggplot(mtcars, aes(x=hp, y= mpg, group=tran)) + geom_point(aes(colour=tran)) 
ggplot(mtcars, aes(x=hp, y= mpg, group=tran)) + geom_point(aes(colour=tran)) + geom_smooth(aes(colour=tran))
ggplot(mtcars, aes(x=hp, y= mpg, group=tran)) + geom_smooth(aes(colour=tran)) + scale_y_continuous(limits=c(12,60))

我想做的是绘制一条平滑的曲线,但是通过限制y轴的比例来放大".但是,当计算出平滑曲线是什么时,ggplot似乎排除了超出比例限制的任何数据.是的,这似乎合乎逻辑,但是我将如何看到自己想要看到的东西?在我的真实数据中,(原始)y值在5到14之间,但是平滑曲线完全在7到9之间.因此顶部有很多空白.当我将其设置为c(7,9)时,它不再使用该范围之外的点来计算平滑曲线,因此得到了另一条曲线.

What I want to do is draw a smoothed curve but 'zoom in' by restricting the scale of the y axis. However it seems ggplot excludes any data outside the scale limits when working out what the smoothed curves are. Yes, this seems logical, but how would I see what I want to see? In my real data, the (raw) y values range between 5 and 14, but the smoothed curves lie entirely between 7 and 9. So there is a lot of empty space at the top. When I set it to c(7,9) it no longer uses points outside of that range to calculate the smooth curve, and hence I get a different curve.

我无法提供数据,但您可以在此示例中看到.看看在最后一个绘图中如何删除两个点,并且"Man"曲线的后半部分与原始图中的不一样.

I can't provide by data but you can see this in this example. See how in the last plot, two points are dropped off and the last half of the 'Man' curve is not that same as it was in the original graph.

问题

如果scale_y_continuous限制了用于构建平滑曲线(逻辑)的数据值,那么如何使用所有数据绘制曲线,然后在其上放大"(扭曲y轴).

If scale_y_continuous limits the data values that are used to construct the smoothed curve (logical), how can you draw the curve using all the data and then 'zoom in' on it (wrt the y axis).

让我知道是否不清楚.

谢谢

推荐答案

使用 coord_cartesian 代替 scale_y_continuous

ggplot(mtcars, aes(x=hp, y= mpg, group=tran)) + 
geom_smooth(aes(colour=tran))  +
coord_cartesian(ylim = c(12,60))

这篇关于geom_smooth()-缩放y轴,从平滑中丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆