Search code examples
rggplot2facet

Include aggregate group in facetted plot


I have the following data and graph

c1 <- seq(1,100,1)
c2 <- rnorm(100,0,1)
c3 <- rep(c(1,2,3,4),25)
dat <- data.frame(cbind(c1,c2,c3))

library(ggplot2)

ggplot(dat, aes(x = c1, y = c2, color = as.factor(c3))) +
  geom_line()

How can I include a line (also in the legend) that depicts the aggregate of c3, i.e. the line that one would get using

ggplot(dat, aes(x = c1, y = c2)) +
  geom_line()

Thanks in advance.


Solution

  • this could be a simple workaround:

    dat0<-dat
    dat0$c3="All"
    dat1<-rbind(dat,dat0)
    ggplot(dat1, aes(x = c1, y = c2, color = as.factor(c3))) +geom_line()