整个组的趋势线 - R

trend line for a whole group - R

我正在尝试为所有组绘制一条趋势线,但它为每个组绘制了一条趋势线。我正在使用的相同代码我把它放在这里(带有 iris 数据集):

iris %>%
  mutate(id = as.numeric(rownames(iris))) %>%
  select(id, Sepal.Length, Sepal.Width, Petal.Length) %>%
  reshape(., direction = "long", varying = names(.)[2:4], v.names = "valor", idvar = c("id"), timevar = "tipo", times= colnames(.[2:4])) %>%
  ggplot(aes(x=id, y=valor, fill=tipo)) +
  geom_area() +
  geom_smooth(method = "lm")

我的数据帧的输出图像(没有 geom_smooth() 行)

我尝试添加趋势线:

  geom_smooth(method = "lm")

但它为每个组添加了一条趋势线,而我只需要一条趋势线作为总数。

为每个单独的 geom 设置 aes 映射:

iris %>%
  mutate(id = rownames(iris)) %>%
  select(id, Sepal.Length, Sepal.Width, Petal.Length) %>%
  reshape(., direction = "long", varying = names(.)[2:4], v.names = "valor", idvar = c("id"), timevar = "tipo", times= colnames(.[2:4])) %>%
  mutate(id = as.numeric(id)) %>%
  ggplot() +
  geom_area(aes(x=id, y=valor, fill=tipo)) +
  geom_smooth(aes(x=id, y=valor), method = "lm")

(我需要添加一个额外的变异来将 id 更改为数字以使您的代码正常工作)