r - ggplot2 两条线之间的差异

标签 r ggplot2 difference

我想将两行之间的差异添加到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() 内部使用 xy 而不是实际的变量名称(函数会知道您的 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))

enter image description here

关于r - ggplot2 两条线之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22386555/

相关文章:

r - 在 `for` 循环中创建一组绘图

java - 这两个通用函数之间的区别

sql - 如何根据 PostgreSQL 中的行差异有条件地合并行?

roxygen2:即使在使用 @export 后,函数也不会导出到 NAMESPACE

r - 匹配特定列中的前一行并在 R 中执行计算

R float 精度在从字符转换时丢失

r - ggplot2:图下方的中心图例而不是面板区域

r - 控制 ggplot2 图中点的相对大小

html - css - 显示:none; and visibility:hidden;?有什么区别

R:根据另一列从行中减去值