【问题标题】:Multiple formulas overlaid on a scatterplot多个公式叠加在散点图上
【发布时间】:2019-09-15 06:09:38
【问题描述】:

每个人。我有一组我称之为df_train 的数据和各种感兴趣的回归公式。供您参考:

df_train <- data.frame(
  x = c(0, 0.111111, 0.222222, 0.333333, 0.444444, 0.555556, 0.666667, 0.777778, 0.888889, 1),
  y = c(0.349486, 0.830839, 1.007332, 0.971507, 0.133066, 0.166823, -0.848307, -0.445686, -0.563567, 0.261502))

forms <- c("y~1", 
           "y~x",
           "y~poly(x, 2, raw=TRUE)",
           "y~poly(x, 3, raw=TRUE)",
           "y~poly(x, 4, raw=TRUE)",
           "y~poly(x, 5, raw=TRUE)",
           "y~poly(x, 6, raw=TRUE)",
           "y~poly(x, 7, raw=TRUE)",
           "y~poly(x, 8, raw=TRUE)",
           "y~poly(x, 9, raw=TRUE)")

我想创建一个与此代码生成的图表类似的图表,但以更简洁的方式。

df_train_exp <- df_train %>%
  add_column(., forms = forms) %>%
  expand(., x, forms) %>%
  left_join(., df_train) %>%
  select(., x, y, forms) %>%
  group_by(., forms) %>%
  arrange(., forms, x) %>%
  ungroup(.)

ggplot(df_train_exp, aes(x = x, y = y)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, formula = forms[1], size = 0.5) +
  geom_smooth(method = "lm", se = FALSE, formula = forms[2], size = 0.5) +
  geom_smooth(method = "lm", se = FALSE, formula = forms[3], size = 0.5) +
  geom_smooth(method = "lm", se = FALSE, formula = forms[4], size = 0.5) +
  geom_smooth(method = "lm", se = FALSE, formula = forms[5], size = 0.5) +
  geom_smooth(method = "lm", se = FALSE, formula = forms[6], size = 0.5) +
  geom_smooth(method = "lm", se = FALSE, formula = forms[7], size = 0.5) +
  geom_smooth(method = "lm", se = FALSE, formula = forms[8], size = 0.5) +
  geom_smooth(method = "lm", se = FALSE, formula = forms[9], size = 0.5) +
  theme_classic()

我尝试了以下但无济于事。

ggplot(df_train_exp, aes(x = x, y = y, color = forms)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, formula = forms, size = 0.5) +
  theme_classic()

我将不胜感激任何帮助或指导,并提前感谢那些比我更精通 R 的人。

【问题讨论】:

  • 这些是字符值。如果您希望它们成为实际的 R 公式,那么您可能需要使用 as.formula。
  • @42- 是否有必要使用as.formula 转换它们?有无我都试过了,都没有看到任何效果。
  • 我真的不知道是否需要转换。 ggplot2语义有很多非标准的评估机制。

标签: r ggplot2


【解决方案1】:

这是一个完全可重现的解决方案:

library(ggplot2)
library(purrr)

df_train <- data.frame(x = c(0, 0.111111, 0.222222, 0.333333, 0.444444, 0.555556, 0.666667, 0.777778, 0.888889, 1),
                       y = c(0.349486, 0.830839, 1.007332, 0.971507, 0.133066, 0.166823, -0.848307, -0.445686, -0.563567, 0.261502))

forms <- c("y ~ 1", 
           "y ~ x",
           "y ~ poly(x = x, degree = 2, raw = TRUE)",
           "y ~ poly(x = x, degree = 3, raw = TRUE)",
           "y ~ poly(x = x, degree = 4, raw = TRUE)",
           "y ~ poly(x = x, degree = 5, raw = TRUE)",
           "y ~ poly(x = x, degree = 6, raw = TRUE)",
           "y ~ poly(x = x, degree = 7, raw = TRUE)",
           "y ~ poly(x = x, degree = 8, raw = TRUE)",
           "y ~ poly(x = x, degree = 9, raw = TRUE)")

ggplot(data = df_train,
       mapping = aes(x = x,
                     y = y)) +
  geom_point() +
  map(.x = forms,
      .f = ~ geom_smooth(mapping = aes(colour = paste("Model", which(x = (forms == .x)))),
                         method = "lm",
                         se = FALSE,
                         formula = .x,
                         size = 0.5)) +
  scale_colour_viridis_d(name = "legend",
                         aesthetics = "colour") +
  theme_classic()

reprex package (v0.3.0) 于 2019-09-15 创建

希望这会有所帮助。

【讨论】:

  • 当我需要purrr 映射工具时,我还没有形成直觉。这个问题中的什么使得映射变得必要?我仍然不明白为什么我尝试的解决方案不起作用。我将数据以长格式放入,并将公式设为颜色变量,那么我尝试的解决方案到底行不通?
  • @StatGuy45 随意将map(...) 部分替换为对应的lapply(...) 调用:lapply(X = forms, FUN = function(t) geom_smooth(mapping = aes(colour = paste("Model", which(x = (forms == t)))), method = "lm", se = FALSE, formula = t, size = 0.5))。我使用了map,因为在我看来,您正在寻求tidyverse 解决方案。
  • @StatGuy45 关键是您必须在list 中添加多个图层。其实我是从这里知道的:stackoverflow.com/a/56990160/11117265
猜你喜欢
  • 2011-05-28
  • 1970-01-01
  • 2021-08-03
  • 1970-01-01
  • 2020-10-10
  • 2018-03-17
  • 2014-07-14
  • 1970-01-01
  • 2012-06-26
相关资源
最近更新 更多