slipleft
slipleft

Reputation: 385

Scatterplot trendline by variable AND overall

How can I include an overall trendline using GGPLOT in addition to trendlines by a defined variable?

My data is plotting closeness (y-axis) by tenure (x-axis). Each observation has an additional variable, "Gender".

The first 2 rows of data are below. My current code includes color by "Gender", along with a trendline for each (M / F). Is it possible to simultaneously include a third, 'aggregate' trendline for the complete data (i.e. not subset by gender)?

No.     Gender     Tenure     Closeness
1          M          3           .3
2          F          5           .5


ggplot(B, aes(x=Tenure, y = Closeness, color=Gender)) + 
geom_point(alpha = .5) + 
geom_smooth(se=TRUE, alpha=.2, size=.2, aes(fill=Gender))

Upvotes: 1

Views: 1122

Answers (1)

user3603486
user3603486

Reputation:

Just set group to NULL in the specific call to geom_smooth. This works for recent versions of ggplot. For your version you might have to also set color and fill to NULL.

ggplot(B, aes(x=Tenure, y = Closeness, color=Gender)) + 
geom_point(alpha = .5) + 
geom_smooth(se=TRUE, alpha=.2, size=.2, aes(fill=Gender)) +
geom_smooth(aes(group=NULL))

Upvotes: 1

Related Questions