Rplot_ly通过带有颜色和符号的连续交互散点图回归线进行分类

标签 r plotly scatter-plot lm interaction

我需要为两个级别的分类因素(在我的例子中为性别)构建一个带有两条回归线的散点图。散点图应该包含所有观测值,但每个级别的线性模型需要单独绘制。换句话说,以下模型的散点图:

连续A = 截距+连续B + 分类+连续B*分类。

Plotly 不会注册由 add_markers() 指定的颜色,除非删除了 add_lines() 和 add_ribbons() 。如果这不能用 Plotly 完成(一个 bug?),那么可以用 ggplot(也许还有 GGally)完成吗?

此外,我很想看看这是否可以通过更短、更整洁或更漂亮的代码或函数来完成。一次完成所有三个 iris$Species 会更好。

library(plotly)
library(broom)
plot_ly() %>%
  add_lines(data = iris[which(iris$Species=='versicolor'), ],
            y = ~fitted(lm(data = iris[which(iris$Species=='versicolor'), ], Petal.Width ~ Petal.Length)),
            x = ~Petal.Length,
            line = list(color = "red"),
            name = "Versicolor") %>%
  # Plot the 95% CI of slope ribbon
  add_ribbons(data = augment(lm(data = iris[which(iris$Species=='versicolor'), ], Petal.Width ~ Petal.Length)),
              y = ~Petal.Width,
              x = ~Petal.Length,
              ymin = ~.fitted - 1.96 * .se.fit,
              ymax = ~.fitted + 1.96 * .se.fit,
              line = list(color = 'rgba(255, 255, 255, 0.05)'), #get rid of the border line
              fillcolor = 'rgba(255, 0, 0, 0.1)', #red with alpha transparency
              name = "Versicolor (Standard Error)",
              showlegend = FALSE) %>%
  add_lines(data = iris[which(iris$Species=='virginica'), ],
            y = ~fitted(lm(data = iris[which(iris$Species=='virginica'), ], Petal.Width ~ Petal.Length)),
            x = ~Petal.Length,
            line = list(color = "green", dash = "dash"),
            name = "Viginica") %>%
  add_ribbons(data = augment(lm(data = iris[which(iris$Species=='virginica'), ], Petal.Width ~ Petal.Length)),
              y = ~Petal.Width,
              x = ~Petal.Length,
              ymin = ~.fitted - 1.96 * .se.fit,
              ymax = ~.fitted + 1.96 * .se.fit,
              line = list(color = 'rgba(255, 255, 255, 0.05)'), #get rid of the border line
              fillcolor = 'rgba(0, 255, 0, 0.1)', #green with alpha transparency
              name = "Virginica (Standard Error)",
              showlegend = FALSE) %>%
  add_markers(data = iris[which(iris$Species=='versicolor' | iris$Species=='virginica'), ], 
              x = ~Petal.Length, 
              y = ~Petal.Width,
              symbol = ~Species,
              color = ~Species, colors = c("versicolor" = "red", "virginica" = "green")) %>%
  layout(xaxis = list(title = "Petal Length"), yaxis = list(title = "Petal Width"))

Interaction Scatterplot

最佳答案

Plotly does not register the colors specified by add_markers(), unless the add_lines() and add_ribbons() are removed. If this cannot be done with Plotly (a bug?) then can it be done with ggplot (and perhaps GGally)?

您可以单独为不同类别添加标记,即重复调用 add_markers

Also, I would love to see if this can be done with shorter, tidier, or prettier code, or with a function. It would be even better to do all three iris$Species at once.

您可以循环遍历所有三个物种,因此您只需编写一次代码。

library(plotly)
library(broom)

species <-  unique(iris$Species)
colors <- c('(255, 0, 0', '(0, 255, 0', '(0, 0, 255')

p <- plot_ly()

for (i in 1:length(species)) {
  p <- add_lines(p, data = iris[which(iris$Species==species[[i]]), ],
            y = fitted(lm(data = iris[which(iris$Species==species[[i]]), ], Petal.Width ~ Petal.Length)),
            x = ~Petal.Length,
            line = list(color = paste('rgb', colors[[i]], ')')),
            name = species[[i]])
    p <- add_ribbons(p, data = augment(lm(data = iris[which(iris$Species==species[[i]]), ], Petal.Width ~ Petal.Length)),
                y = ~Petal.Width,
                x = ~Petal.Length,
                ymin = ~.fitted - 1.96 * .se.fit,
                ymax = ~.fitted + 1.96 * .se.fit,
                line = list(color = paste('rgba', colors[[i]], ', 0.05)')), 
                fillcolor = paste('rgba', colors[[i]], ', 0.1)'),
                showlegend = FALSE)
    p <- add_markers(p, data = iris[which(iris$Species==species[[i]]), ], 
              x = ~Petal.Length, 
              y = ~Petal.Width,
              symbol = ~Species,
              marker=list(color=paste('rgb', colors[[i]])))
}
p <- layout(p, xaxis = list(title = "Petal Length"), yaxis = list(title = "Petal Width"))

p

enter image description here

关于Rplot_ly通过带有颜色和符号的连续交互散点图回归线进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42708828/

相关文章:

R Shiny 横款

r - 将刻度线标签更改为 plotly 中的特定字符串

python - 散点图python双边缘线

python-3.x - 你能解释一下为什么情节在 J 处停止(在索引 10 处)

r - is.na() 的行为与 is.numeric() 不同——一致性在哪里?

r - 从包中导出所有隐藏函数

r - 使用 R 中的 quarto_render 渲染 qmd 文件的两个问题

r - 为 R : Remove the k that apears on the y axis when the dataset contains numbers larger than 1000 作图

python - Plotly:如何使用下拉菜单选择图形源?

javascript - 使用 d3.js 在散点图中显示靠近鼠标指针的工具提示