水平线代表R和ggplot平均值的散点图

时间:2018-09-18 04:57:53

标签: r ggplot2 scatter-plot

下面的代码为每个组生成带有回归线的散点图。代替倾斜的回归线,可以绘制表示每组y值平均值的水平线吗?我尝试将公式参数修改为“ y〜0 * x”,但没有想到其他显而易见的用途。

谢谢

ggplot(data = iris, aes(y = Sepal.Length, x = Sepal.Width, colour = Species)) + geom_point() + 
geom_smooth(method = 'lm', formula = y ~ x , se = F)

1 个答案:

答案 0 :(得分:2)

我们可以将公式指定为y ~ 1

library(ggplot2)

ggplot(data = iris, aes(y = Sepal.Length, x = Sepal.Width, colour = Species)) + 
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ 1)

enter image description here

相关问题