Dan_yef Dan_yef - 1 year ago 105
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?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download