Dan_yef Dan_yef - 5 months ago 36
R Question

Smooth line to the full datase (faceting) ggplot2

I want to add smooth line of the full dataset to each facet.

However, the following code add smooth line different(of each facet) smooth line to each facet.

ggplot(mpg2,aes(displ,hwy)) + geom_point() + facet_wrap(~class) + geom_smooth(se = FALSE)

How can I fix that?


You can add the geom_smooth layer using a dataset that doesn't contain the faceting variable. So remove class from the dataset.

ggplot(mpg, aes(displ, hwy)) + 
    geom_point()  + 
    facet_wrap(~class) + 
    geom_smooth(data = mpg[,1:10], se = FALSE)