Scatterplot趋势线由变量AND整体

时间:2016-10-06 22:57:59

标签: r ggplot2

除了定义变量的趋势线外,如何使用GGPLOT包含整体趋势线?

我的数据是根据任期(x轴)绘制贴近度(y轴)。每个观察都有一个额外的变量,"性别"。

前两行数据如下。我当前的代码包括" Gender"的颜色,以及每个(M / F)的趋势线。是否可以同时包含第三个'聚合'完整数据的趋势线(即不按性别分列)?

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))

1 个答案:

答案 0 :(得分:1)

只需在group的特定通话中将NULL设为geom_smooth即可。这适用于最新版本的ggplot。对于您的版本,您可能还需要设置颜色并填充到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))