情节 - 如何使用R添加每种颜色的多个回归?

发布于 2025-02-03 19:50:59 字数 526 浏览 4 评论 0原文

我的情节是将所有回归线彼此堆叠在一起。


mod = lm(weight_kg ~ height_cm, data=rugby_data) 

fig2 <- plot_ly(rugby_data, x = ~height_cm, y = ~weight_kg, type="scatter", mode="markers", color = rugby_data$continent) %>% 
        add_lines(x = rugby_data$height_cm,y = fitted(mod), name="fitted", mode = "lines") %>%
  layout(title = 'Height Vs. Weight Scatter Plot', plot_bgcolor = "#e5ecf6")

fig2

代码和图的图像

My plot is stacking all the regression lines on top of each other.


mod = lm(weight_kg ~ height_cm, data=rugby_data) 

fig2 <- plot_ly(rugby_data, x = ~height_cm, y = ~weight_kg, type="scatter", mode="markers", color = rugby_data$continent) %>% 
        add_lines(x = rugby_data$height_cm,y = fitted(mod), name="fitted", mode = "lines") %>%
  layout(title = 'Height Vs. Weight Scatter Plot', plot_bgcolor = "#e5ecf6")

fig2

Image of code and plot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮坠入山谷 2025-02-10 19:50:59

您的lm对象不适合组(我猜是concodant),

如果您的目的是绘制点和回归行,

您可以尝试使用ggplot2 ggplot2 < /代码>。

由于您没有提供您的数据,因此我以iris为例。

library(dplyr)
library(ggplot2)

iris %>%
  ggplot(aes(Sepal.Width, Sepal.Length, group = Species, color = Species)) +
  geom_smooth(method = "lm") +
  geom_point()

Your lm object did not fit model by groups(I guess it's continent)

If your purpose is to plot the points and regression lines,

you may try using ggplot2.

As you did not provide your data, I use iris as an example.

library(dplyr)
library(ggplot2)

iris %>%
  ggplot(aes(Sepal.Width, Sepal.Length, group = Species, color = Species)) +
  geom_smooth(method = "lm") +
  geom_point()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文