我想将两行之间的差异添加到ggplot2
中。
在此示例中,位于由 x2
定义的两个组的行之间。
如何才能做到这一点?
y=rbinom(100,1,.4)
x1=rnorm(100, 3, 2)
x2=rbinom(100, 1, .7)
sub = data.frame(y=y, x1=x1, x2=x2)
ggplot(sub, aes(x1, y, color = x2)) +
stat_smooth(method = "glm", family = binomial, formula = y ~ poly(x1,3))
最佳答案
您应该在代码中更改两件事。首先,在 stat_smooth()
内部使用 x
和 y
而不是实际的变量名称(函数会知道您的 x
> 值为x1
)。其次,将 x2
包裹在 factor()
内以具有两种不同的颜色。
ggplot(sub, aes(x=x1, y=y, color = factor(x2))) +
stat_smooth(method = "glm", family = binomial, formula = y ~ poly(x,3))
关于r - ggplot2 两条线之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22386555/