【问题标题】:Saturation curve in ggplotggplot中的饱和曲线
【发布时间】:2021-05-06 13:53:49
【问题描述】:

我之前尝试使用 loess 和 lm 方法 (ggplot2) 拟合我的数据,但是这些方法都不适合。现在我正在尝试将饱和曲线拟合到数据中,但找不到这样做的方法。谷歌搜索了一段时间后,我想知道这是否可以使用 ggplot2。否则我考虑过拟合逻辑回归,并尝试了这个,不幸的是,这给了我以下错误:

这将使用 loess 方法生成想要的图形。如何用饱和曲线绘制它?

specprec <- ggplot(data=master, aes(prec_average, binomial_c))+
  geom_point(size=1.3, shape=16)+
  theme_bw(base_size = 14)+
  theme(aspect.ratio=1)+
  labs(x="Mean precipitation", y="Species richness")+
  stat_smooth(method="loess")

这是我尝试拟合逻辑回归曲线的方法:

m <- glm(prec_average ~ binomial_c, family = gaussian, data = master)
p_specprec <-  augment(m, type.predict = "response")
head(p_specprec)
 
 base <-
   ggplot(p_specprec, aes(x = prec_average, y=m)) +#   geom_line(aes(y = m), color = "blue") +
   labs(x = "Mean precipitation", y = "Species richness")

Error: Don't know how to automatically pick scale for object of type glm/lm. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (4538): y

任何提示都会有很大帮助!提前致谢!

【问题讨论】:

  • 能否请您提供您的数据?
  • 嗯,我认为至少有一些问题是试图将您的模型 m 传递给 y。第一条消息是因为 y 期待一个变量,而不是一个模型。由于您使用的是augment()(可能来自broom?),因此您已将预测值添加到数据集中。尝试将它们作为您的 y 变量?要从人们那里获得更多帮助:),请尝试添加一个可重现的示例:stackoverflow.com/questions/5963269/…

标签: r ggplot2 curve


【解决方案1】:

出现错误是因为您尝试绘制模型 m 而不是数据框 p_specprec 中的列。

由于我无权访问您的数据,因此我使用了 mtcars 并重新标记了一些列。

library(ggplot)
master <- mtcars
master<- master %>% rename(prec_average = mpg, binomial_c = disp)

您还需要包含库 broom 才能使增强功能起作用。

library(broom)

定义模型,并将结果转化为数据框。

m <- glm(prec_average ~ binomial_c, family = gaussian, data = master)
p_specprec <-  augment(m, type.predict = "response")

在这里,我们使用 ggplot 指定要在美学中使用的数据和列。 我猜你想将prec_average 与位于p_specprec$.fitted 的拟合值进行对比,还要注意在geom_line() 中你不需要再次指定美学。

ggplot(p_specprec, aes(x = prec_average, y=.fitted)) + 
  geom_line(colour = 'blue')+
  labs(x = "Mean precipitation", y = "Species richness")

【讨论】:

    猜你喜欢
    • 2016-11-06
    • 2022-01-18
    • 1970-01-01
    • 2017-11-06
    • 2017-08-03
    • 2016-05-17
    • 2014-08-09
    • 1970-01-01
    • 2018-08-16
    相关资源
    最近更新 更多