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))
而不明确指定限制。
发布于 2011-10-26 04:15:04
手动拟合平滑模型为您提供了更大的灵活性,以实现此类型和其他类型的自定义。对于大多数项目,我开始使用内联界面,但当我需要其他调整时,通常最终不得不切换到手动计算。
另见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)
但是,这仍然不适用于facet,因为您只能指定scales='free'
,而不能直接指定实际限制。
https://stackoverflow.com/questions/7857020
复制相似问题