0

I want to plot multiple facets with several lines each. In every facet, for the set of line plots, I would like to show a kind of smooth line representing a general trend for all those lines in a given particular facet. Does anybody know if it is possible? I have tried so far something like this:

ggplot(data = mydata, aes(x = x, y = y, group = group)) + geom_line('GRAY') + facet_wrap(~ class) + geom_smooth('loess') (see figure)

However, what I observe is another line for every line plot within each facet, instead of a single line within every facet.

Thanks in advance

Paco el Cuqui
  • 183
  • 3
  • 13

2 Answers2

1
... +
geom_smooth(aes(group=class), method="lm") +
...

Should make one line pr. facet

Zoe
  • 27,060
  • 21
  • 118
  • 148
pallevillesen
  • 735
  • 6
  • 14
0

This is an example with the iris dataset:

    gg <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) + 
            geom_line() + 
            facet_wrap(~ Species) +
            geom_smooth(data = within(iris, Species <- NULL), fill = "red")
    gg 
Valter Beaković
  • 3,140
  • 2
  • 20
  • 30
  • This is not an answer. If you want to help OP (he or she should to it him/herself, though), feel free to edit their question. – Roman Luštrik Oct 25 '16 at 13:24
  • Maybe I explained myself wrong, but it is not exactly what I'm trying to do. I have multiple lines but within the same facet, and multiple facets. In that example with the iris dataset, you have one line per facet, whereas I have many. – Paco el Cuqui Oct 25 '16 at 13:27
  • This is why I asked for a sample of the original data set. The iris example was an attempt to help while waiting for the sample.... – Valter Beaković Oct 25 '16 at 13:29