ggplot-绘制GEOM_SMOOTH使用伽马误差分布时的反向y轴

发布于 2025-02-12 21:51:54 字数 850 浏览 0 评论 0原文

我正在尝试使用伽马错误分布来绘制geom_smooth

library(ggplot)

data <- data.frame(x = 1:100, y = (1:100 + runif(1:100, min = 0, max = 50))^2)

p <- ggplot(data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = 'glm', method.args = list(family = Gamma(link = "log")))

我也想逆转y轴,但是使用scale_y_reverse,但是这会导致伽马分布失败,因为它不能应用于负值。如何扭转此图的Y轴?

p + scale_y_reverse()
Warning message:
Computation failed in `stat_smooth()`:
non-positive values not allowed for the 'Gamma' family 

I am trying to plot a geom_smooth using a gamma error distribution.

library(ggplot)

data <- data.frame(x = 1:100, y = (1:100 + runif(1:100, min = 0, max = 50))^2)

p <- ggplot(data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = 'glm', method.args = list(family = Gamma(link = "log")))

geom smooth

I also want to reverse the y-axis however using scale_y_reverse, but this causes the Gamma distribution to fail as it can't be applied to negative values. How can I reverse the y-axis for this plot?

p + scale_y_reverse()
Warning message:
Computation failed in `stat_smooth()`:
non-positive values not allowed for the 'Gamma' family 

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

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

发布评论

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

评论(1

追风人 2025-02-19 21:51:54

我不确定是否有构建方法可以调用geom_smooth的预测值scale> scale_y_reverse工作。

这是可视化回归模型的更传统的方法,即构造,预测和绘图。

library(broom)
model <- glm(y ~ x, data, family = Gamma(link = "log"))
new <- augment(model, se=TRUE) 

ggplot(new, aes(x, y)) + 
  geom_point() + 
  geom_line(aes(y=exp(1)^.fitted)) + 
  geom_line(aes(y=exp(1)^(.fitted + .se.fit)), linetype="dashed") + 
  geom_line(aes(y=exp(1)^(.fitted - .se.fit)), linetype="dashed") + 
  scale_y_reverse()

I'm not sure if there are build-in methods to call out the predicted values of geom_smooth for scale_y_reverse to work.

Here's the more conventional method with visualizing of regression models, i.e. construct, predict and plot.

library(broom)
model <- glm(y ~ x, data, family = Gamma(link = "log"))
new <- augment(model, se=TRUE) 

ggplot(new, aes(x, y)) + 
  geom_point() + 
  geom_line(aes(y=exp(1)^.fitted)) + 
  geom_line(aes(y=exp(1)^(.fitted + .se.fit)), linetype="dashed") + 
  geom_line(aes(y=exp(1)^(.fitted - .se.fit)), linetype="dashed") + 
  scale_y_reverse()

enter image description here

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