如何在R ggplot2中的geom_smooth(facet_wrap)中传递多个公式?
我想为每个因素中的每个因素(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 in
stat_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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个
geom_smooth()
只能有一个公式。您需要添加三个不同的geom_smooth
图层。您可以手动执行此操作或者您可以使用
lapply
来帮助如果您希望每个面板有不同的行,那么您可以过滤每个面板的数据。在这里,我们使用
maply
帮助器并对每行数据进行子集化。You can only have one formula per
geom_smooth()
. You'll need to add three differentgeom_smooth
layers. YOu can do that manuallyOr you can use
lapply
to helpIf 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.