【发布时间】:2016-07-31 23:58:49
【问题描述】:
我正在尝试使用 R 中的 geom_smooth() 覆盖多条趋势线。我目前有此代码。
ggplot(mtcars2, aes(x=Displacement, y = Variable, color = Variable))
+ geom_point(aes(x=mpg, y = hp, col = "Power"))
+ geom_point(aes(x=mpg, y = drat, col = "Drag Coef."))
(mtcars2 是 mtcars 的规范化形式)
我正在尝试使用 geom_smooth(method='lm') 为这两个变量绘制两条趋势线。有什么想法吗?
(奖励:如果可能,我还想实现 'shape=1' 参数来区分变量。以下方法不起作用)
geom_point(aes(x=mpg, y = hp, col = "Power", shape=2))
更新 我设法做到了。
ggplot(mtcars2, aes(x=Displacement, y = Variable, color = Variable))
+ geom_point(aes(x=disp, y = hp, col = "Power"))
+ geom_point(aes(x=disp, y = mpg, col = "MPG"))
+ geom_smooth(method= 'lm',aes(x=disp, y = hp, col = "Power"))
+ geom_smooth(method= 'lm',aes(x=disp, y = mpg, col = "MPG"))
看起来像这样。
但这是一段丑陋的代码。如果有人能让这段代码看起来更漂亮,那就太好了。另外,我还不能实现 'shape=2' 参数。
【问题讨论】:
-
mtcars2与mtcars有何不同? -
我对整个数据集进行了标准化。相应地更新了问题。
标签: r ggplot2 scatter-plot lm