ggplot2——自动放大geom_smooth(使用coord_cartesian)

发布于 2024-12-11 02:26:27 字数 558 浏览 0 评论 0原文

geom_smooth 很棒,很大程度上是因为它平均了很多变化。然而,正因为如此,当它缩小时,很难看出它在 x 轴上的变化。我正在生成大约 1000 个图表,我需要通过 coord_cartesian 放大 ggplot2。然而,每个图表都有不同的缩放限制。有没有办法让 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))

而不明确指定限制。

geom_smooth is great, in large part because it averages out a lot of variation. However, because of this, it's difficult to see how it varies over the x-axis when it is zoomed out. I am producing about 1000 graphs where I need to have ggplot2 zoom in via coord_cartesian. However, each graph would have different zoom limits. Is there a way I can ask ggplot2 to zoom in to fit the smooth? I am interested in solutions for both zooming in on just the geom_smooth line and the geom_smooth line plus SE shaded area.

For example, I would be interested in knowing how I could turn this:

ggplot(data=mtcars, aes(y=qsec,x=wt)) + geom_point() + geom_smooth()

into something like this:

ggplot(data=mtcars, aes(y=qsec,x=wt)) + geom_point() + geom_smooth() + coord_cartesian(ylim = c(15,20))

without explicitly specifying the limits.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2024-12-18 02:26:27

手动拟合平滑模型可以让您更加灵活地实现这种自定义和其他类型的自定义。对于大多数项目,我一开始使用内联界面,但当我需要其他调整时,通常最终不得不切换到手动计算。

另请参阅 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',而不是直接的实际限制。

Fitting your smoothing models manually gives you much more flexibility for attaining this and other types of customization. For most projects, I start off using the in-line interface, but then usually end up having to switch to manual calculation when I need other tweaks.

See also §9.1.1 in Hadley's book.

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)

enter image description here

However, this still doesn't work for facets because you can only specify, for example, scales='free', and not the actual limits directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文