2017-04-20 59 views
0

我正在尝试为每个方面(按性别)基于exercise = 0或exercise = 1创建两个不同的行。第一个代码没有facet_wrap,基于性别的两行代码是不同的。第二个代码是facet_wrap,两行似乎是同一行。我怎样才能改变代码,使两条线在每个方面都有所不同?如何根据每个方面的因子创建两个不同的回归线? R,ggplot2

ggplot(cdc, aes(weight,wtdesire, color = exercise, group = 
interaction(gender,exercise))) + geom_point(alpha = 1/5) + 
geom_smooth(method = lm, aes(linetype=exercise)) 

生产:facet

然而,当我添加facet_wrap两条线每个面似乎是相同的。

ggplot(cdc, aes(weight,wtdesire, color = exercise, group = 
interaction(gender,exercise))) + geom_point(alpha = 1/5) + 
geom_smooth(method = lm, aes(linetype=exercise)) + facet_wrap(~gender) 

生产:second

+0

你可能需要在第二个情节只使用'组=锻炼'。 – lbusett

+0

@LoBu我也试过,但它没有改变任何东西... –

回答

0

@LoBu溶液是正确的。下面是一个例子使用mtcars数据:

ggplot(mtcars, aes(hp, mpg, group=interaction(vs, am))) + 
     geom_point(alpha = 0.2) + 
     geom_smooth(method = lm, aes(linetype=as.factor(vs))) 

enter image description here

ggplot(mtcars, aes(hp, mpg, group=vs)) + 
     geom_point(alpha = 0.5) + 
     geom_smooth(method = lm, aes(linetype=as.factor(vs))) + 
     facet_wrap(~am) 

enter image description here

+0

谢谢!我通过as.factor而不是连续变量进行了分组。 –

相关问题