你不应该重做你的全部aes
in the geom_point
添加另一层,只需移动形状aes
to the geom_point
call:
df <- data.frame(x=1:10,y=1:100+5,grouping = c(rep("a",10),rep("b",10)))
ggplot(df,aes(x=x,y=y)) +
geom_point(aes(shape=grouping)) +
stat_smooth(method=lm)
EDIT:
为了帮助您发表评论:
because annotate
无论如何,对我来说,最终每个方面都会有相同的标签。我喜欢做迷你data.frame
其中有用于分面的变量和分面级别,另一列代表我要使用的标签。在这种情况下,标签数据框被称为dfalbs
.
然后用它来标记数据框以单独标记各个方面,例如
df <- data.frame(x=1:10,y=1:10,grouping =
c(rep("a",5),rep("b",5)),faceting=c(rep(c("oneR2","twoR2"),5)))
dflabs <- data.frame(faceting=c("oneR2","twoR2"),posx=c(7.5,7.5),posy=c(2.5,2.5))
ggplot(df,aes(x=x,y=y,group=faceting)) +
geom_point(aes(shape=grouping),size=5) +
stat_smooth(method=lm) +
facet_wrap( ~ faceting) +
geom_text(data=dflabs,aes(x=posx,y=posy,label=faceting))