簡體   English   中英

Plotly - 如何使用 R 為每種顏色添加多個回歸?

[英]Plotly - how to add multiple regressions for each color using R?

我的情節是將所有回歸線堆疊在一起。


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

代碼和繪圖的圖像

您的lm對象不適合按組划分的模型(我猜是continent

如果您的目的是繪制點和回歸線,

您可以嘗試使用ggplot2

由於您沒有提供數據,我以iris為例。

library(dplyr)
library(ggplot2)

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

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM