如何在R ggplot2中的geom_smooth(facet_wrap)中传递多个公式?

发布于 2025-01-20 21:22:31 字数 738 浏览 3 评论 0原文

我想为每个因素中的每个因素(var.test)拟合三个不同的功能。我尝试了以下方法,但是我会发现一个错误,该错误读取警告消息:1:计算失败 stat_smooth():无效公式。有其他方法可以立即通过多个公式阅读吗?

set.seed(14)
df <- data.frame(
  var.test = c("T","T","T","T","M","M","M","M","A","A","A","A"),
  val.test = rnorm(12,4,5),
  x = c(1:12)
)

my.formula <- c(y~x + I(x^2), y~x, y~x + I(x^2))

ggplot(df, aes(x = x, y = val.test)) + geom_point() +
  geom_smooth(method="glm", formula = my.formula, 
              method.args = list(family = "poisson"), color = "black" ) + facet_grid(.~var.test)

I want to fit three different functions for each of these factors (var.test). I tried the following method but I get an error that reads Warning messages: 1: Computation failed instat_smooth():invalid formula. Any other way to get multiple formulae to read be passed at once?

set.seed(14)
df <- data.frame(
  var.test = c("T","T","T","T","M","M","M","M","A","A","A","A"),
  val.test = rnorm(12,4,5),
  x = c(1:12)
)

my.formula <- c(y~x + I(x^2), y~x, y~x + I(x^2))

ggplot(df, aes(x = x, y = val.test)) + geom_point() +
  geom_smooth(method="glm", formula = my.formula, 
              method.args = list(family = "poisson"), color = "black" ) + facet_grid(.~var.test)

enter image description here

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

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

发布评论

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

评论(1

猫弦 2025-01-27 21:22:31

每个 geom_smooth() 只能有一个公式。您需要添加三个不同的 geom_smooth 图层。您可以手动执行此操作

ggplot(df, aes(x = x, y = val.test)) + 
  geom_point() +
  geom_smooth(method="glm", formula = my.formula[[1]], method.args = list(family = "poisson"), color = "black" ) + 
  geom_smooth(method="glm", formula = my.formula[[2]], method.args = list(family = "poisson"), color = "black" ) + 
  geom_smooth(method="glm", formula = my.formula[[3]], method.args = list(family = "poisson"), color = "black" ) + 
  facet_grid(.~var.test)

或者您可以使用 lapply 来帮助

ggplot(df, aes(x = x, y = val.test)) + 
  geom_point() +
  lapply(my.formula, function(x) geom_smooth(method="glm", formula = x, 
              method.args = list(family = "poisson"), color = "black" )) + 
  facet_grid(.~var.test)

Output

如果您希望每个面板有不同的行,那么您可以过滤每个面板的数据。在这里,我们使用 maply 帮助器并对每行数据进行子集化。

ggplot(df, aes(x = x, y = val.test)) + 
  geom_point() +
  mapply(function(x, z) geom_smooth(method="glm", data=function(d) subset(d, var.test==z), formula = x, 
              method.args = list(family = "poisson"), color = "black" ),
         my.formula, c("A","M","T")) + 
facet_grid(.~var.test)

You can only have one formula per geom_smooth(). You'll need to add three different geom_smooth layers. YOu can do that manually

ggplot(df, aes(x = x, y = val.test)) + 
  geom_point() +
  geom_smooth(method="glm", formula = my.formula[[1]], method.args = list(family = "poisson"), color = "black" ) + 
  geom_smooth(method="glm", formula = my.formula[[2]], method.args = list(family = "poisson"), color = "black" ) + 
  geom_smooth(method="glm", formula = my.formula[[3]], method.args = list(family = "poisson"), color = "black" ) + 
  facet_grid(.~var.test)

Or you can use lapply to help

ggplot(df, aes(x = x, y = val.test)) + 
  geom_point() +
  lapply(my.formula, function(x) geom_smooth(method="glm", formula = x, 
              method.args = list(family = "poisson"), color = "black" )) + 
  facet_grid(.~var.test)

Output

If you want different lines per panel, then you can filter your data for each panel. Here we use an mapply helper and subset the data for each line.

ggplot(df, aes(x = x, y = val.test)) + 
  geom_point() +
  mapply(function(x, z) geom_smooth(method="glm", data=function(d) subset(d, var.test==z), formula = x, 
              method.args = list(family = "poisson"), color = "black" ),
         my.formula, c("A","M","T")) + 
facet_grid(.~var.test)

Different lines per panel

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