I'm trying to make a trend line for the total of the groups, but it draws a trend line for EACH group. The same code I'm using I put it here (with iris dataset):
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")
An image of the output of my dataframe (without geom_smooth()
line)
I have tried adding a trend line with:
geom_smooth(method = "lm")
But it adds a trend line for each group, when I just need one for the total.
Set the aes
mapping for each individual geom:
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")
(I needed to add an additional mutate to change id
to numeric to get your code to work)