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

生物技术极客

我想为这些因素中的每一个(var.test)拟合三个不同的函数。我尝试了以下方法,但出现错误,显示为Warning messages: 1: Computation failed in stat_smooth() :invalid formula有没有其他方法可以一次读取多个公式?

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)

在此处输入图像描述

弗里克先生

每个 只能有一个公式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)

输出

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

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)

每个面板不同的行

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在ggplot2中,如何在facet_wrap中按组添加文本?

如何在R中的ggplot中将图例添加到geom_smooth

使用R中的geom_smooth()在ggplot2图例中混合填充颜色

如何通过ggplot2中的facet_wrap排序多个图

R中的ggplot2:在geom_smooth线下方填充

指定组时,ggplot2中带有facet_wrap的geom_text

R:在 ggplot2 中重新标记 facet_wrap 标题

在ggplot2中绘制“ geom_smooth”时,如何删除SE上的填充区域?

如何使用facet_wrap在R中ggplot多个位置点?

GGplot2:从面板中移除部件 (facet_wrap)

控制facet_wrap ggplot2中的x标签

ggplot中的geom_vline和facet_wrap错误

如何在ggplot2中使用facet_wrap(〜day)设置轴每日时间限制

在ggplot2中,当存在facet_wrap时,如何使所有geom_col条具有相同的颜色,而它们具有相同的值?

如何使用ggplot在R中创建不均匀的facet_wrap网格?

如何使用 R 中的 facet_wrap 使用上下限进行 ggplot 着色?

ggplot:如何从facet_wrap中删除空组?

如何更改Facet_Wrap标签在ggplot中的位置?

使用 R 中 ggplot2 的 facet_wrap 功能對多個數據集進行 Boxplot 比較?

ggplot中的facet_wrap for sf

在R中的facet_wrap ggplot中的方括号上方添加多个标题/文本

ggplot2中的facet_wrap()和facet_grid()有什么区别?

如何在ggplot中的facet_wrap中为每个面板重复x标签?

如何在此自定义facet_wrap中添加geom_quantile图例?

如何在R中的facet_wrap标签中使用上标?

如何在R中创建分组条形图而不是facet_wrap?

如何在 geom_smooth()(来自 ggplot2 包)中为多重非线性回归分配不同的初始值?

如何在ggplot facet_wrap文本中添加数学符号?

如何在自定义 ggplot 函数中调用 facet_wrap()?