简体   繁体   English

R - 使用颜色编码覆盖多个最小二乘图

[英]R - Overlay multiple least squares plots with colour coding

I'm trying to visualize some data that looks like this我正在尝试可视化一些看起来像这样的数据

line1 <- data.frame(x = c(4, 24), y = c(0, -0.42864), group = "group1")
line2 <- data.frame(x = c(4, 12 ,24), y = c(0, 2.04538, 3.4135), group = "group2")
line3 <- data.frame(x = c(4, 12, 24), y = c(0, 3.14633, 3.93718), group = "group3")
line4 <- data.frame(x = c(0, 3, 7, 12, 18), y = c(0, -0.50249, 0.11994, -0.68694, -0.98949), group = "group4")
line5 <- data.frame(x = c(0, 3, 7, 12, 18, 24), y = c(0, -0.55753, -0.66006, 0.43796, 1.38723, 3.17906), group = "group5")

df <- do.call(rbind, list(line1, line2, line3, line4, line5))

What I'm trying to do is plot the least squares line (and points) for each group on the same plot.我要做的是 plot 同一 plot 上每个组的最小二乘线(和点)。 And I'd like the colour of the lines and points to correspond to the group.我希望线条和点的颜色与组相对应。

All I've been able to do is plot the points according to their group我所能做的就是 plot 根据他们的小组得分

ggplot(data = df, aes(x, y, colour = group)) + geom_point(aes(size = 10))

But I have no idea how to add in the lines as well and make their colours correspond to the points that they are fitting.但我不知道如何添加线条并使它们的颜色与它们适合的点相对应。

I'd really appreciate any help with this.我真的很感激这方面的任何帮助。 It's turning out to be so much harder than I though it would be.事实证明它比我想象的要困难得多。

You can simply add a geom_smooth layer to your plot您可以简单地将geom_smooth图层添加到您的 plot

ggplot(data = df, aes(x, y, colour = group)) + geom_point(aes(size = 10)) + 
                                               geom_smooth(method="lm",se=FALSE)

method="lm" specifies that you want a linear model method="lm"指定你想要一个线性 model

se=FALSE to avoid plotting confidence intervals se=FALSE以避免绘制置信区间

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM