In ggplot2, how do I group geom_smooth on facets?

Viewed 818

I'm trying to plot time trends across facets and failing at adding geom_smooth correctly.

Doing this:

economics_mod <- economics_long %>% 
  mutate(type = variable) %>% 
  mutate(type = recode(type, `pce` = "1", `pop` = "1", .default = "2"))

ggplot(economics_mod, aes(date, value01, group = variable)) +
  geom_path() + 
  facet_wrap( ~ type, ncol = 1) +
  geom_smooth()

Gets me here:

enter image description here

Problem is I would like to have one smotthed line per facet, not a series of data.

I tried tinkering with different set ups of aes for path and smooth but none gives any results. Where is the problem?

Update: @mikeck and @Marius solution solves the issue:

library(Matrix)
ggplot(economics_mod, aes(date, value01)) +
  geom_path(aes(group = variable)) + 
  facet_wrap( ~ type, ncol = 1) +
  geom_smooth()
0 Answers
Related