geom_smooth
很棒,很大程度上是因为它平均了很多变化。然而,正因为如此,当它缩小时,很难看出它在 x 轴上的变化。我正在制作大约 1000 个我需要的图表ggplot2
放大通过coord_cartesian
。然而,每个图表都有不同的缩放限制。有什么办法可以询问吗ggplot2
放大以适应平滑?我对同时放大的解决方案感兴趣geom_smooth
线和geom_smooth
线加上 SE 阴影区域。
例如,我有兴趣知道如何改变它:
ggplot(data=mtcars, aes(y=qsec,x=wt)) + geom_point() + geom_smooth()
变成这样的东西:
ggplot(data=mtcars, aes(y=qsec,x=wt)) + geom_point() + geom_smooth() + coord_cartesian(ylim = c(15,20))
没有明确指定限制。
手动拟合平滑模型可以让您更加灵活地实现这种自定义和其他类型的自定义。对于大多数项目,我一开始使用内联界面,但当我需要其他调整时,通常最终不得不切换到手动计算。
另请参阅 Hadley 书中的 §9.1.1。
require(ggplot2)
# Fit smooth manually
fit = loess(qsec ~ wt, data=mtcars)
newx = data.frame(wt=with(mtcars, seq(min(wt), max(wt), len=100)))
pred = predict(fit, newdata=newx, se=T)
pred = cbind(wt=newx, qsec=pred$fit, se=pred$se.fit)
# Calculate limits based on extent of smooth geom
ylims = with(pred, c(floor(min(qsec-se)), ceiling(max(qsec+se))))
# Plot
dev.new(width=5, height=4)
ggplot(data=mtcars, aes(y=qsec, x=wt)) +
geom_point() +
geom_smooth(aes(ymax=qsec+se, ymin=qsec-se), data=pred, stat='identity') +
coord_cartesian(ylim = ylims)
但是,这仍然不适用于构面,因为您只能指定,例如,scales='free'
,而不是直接的实际限制。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)