[英]Difference between two lines ggplot2
我想将两行之间的差异添加到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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.