散点图上覆盖了多个公式

时间:2019-09-15 06:09:38

标签: r ggplot2

每个人。我有一组称为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的人。

1 个答案:

答案 0 :(得分: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创建

希望这会有所帮助。