ggplot2两行之间的区别 [英] Difference between two lines ggplot2

查看:282
本文介绍了ggplot2两行之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两行之间的差异添加到 ggplot2 中。
在此示例中,在由 x2 定义的两个组的行之间。
该怎么做?

I wanted to add the difference between two lines to a ggplot2. In this example, between the lines of the two groups defined by x2. How can this be done?

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 )。其次,在 factor()内包装 x2 使其具有两种不同的颜色。

There are two things you should change in your code. First, inside stat_smooth() use x and y and not the actual variable names (function will know that your x values are x1). Second, wrap x2 inside factor() to have two distinct colors.

ggplot(sub, aes(x=x1, y=y, color = factor(x2))) + 
  stat_smooth(method = "glm", family = binomial, formula = y ~ poly(x,3))

这篇关于ggplot2两行之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆