【问题标题】:Custom R function around plot_ly() with fitted(lm(y~x)) using add_lines()使用 add_lines() 使用拟合的 plot_ly() 自定义 R 函数 (lm(y~x))
【发布时间】:2017-07-09 04:37:35
【问题描述】:

我想在 R 中围绕 plot_ly() 编写一个自定义函数。这样,​​我可以制作一系列具有相同格式和样式的散点图,但不会重复代码。 I used this page as a guide.此代码重现错误:

library(plotly)
my_plot <- function(x, y, ...) {
  require(broom)
  plot_ly(data = mtcars, y = y, x = x, showlegend = FALSE, ...) %>%
    add_markers(y = y) %>%
    add_lines(y = ~fitted(lm(y ~ x))) %>%
    add_ribbons(data = augment(lm(y ~ x, data = mtcars)),
                ymin = ~.fitted - 1.96 * .se.fit,
                ymax = ~.fitted + 1.96 * .se.fit,
                line = list(color = 'rgba(7, 164, 181, 0.05)'),
                fillcolor = 'rgba(7, 164, 181, 0.2)',
                name = "Standard Error")
}
my_plot(y = ~mpg, x = ~disp)

问题线是:

add_lines(y = ~fitted(lm(y ~ x))) %>%

我尝试使用 as.formula(),但错误信息类似。

add_lines(y = ~fitted(lm(as.formula("y ~ x"))) %>%

这是错误信息:

model.frame.default 中的错误(公式 = y ~ x,数据 = mtcars,drop.unused.levels = TRUE): 对象不是矩阵

代码在不是函数时有效:

library(plotly)
library(broom)
plot_ly(data = mtcars, y = ~mpg, x = ~disp, showlegend = FALSE) %>%
  add_markers(y = ~mpg) %>%
  add_lines(y = ~fitted(lm(mpg ~ disp))) %>%
  add_ribbons(data = augment(lm(mpg ~ disp, data = mtcars)),
            ymin = ~.fitted - 1.96 * .se.fit,
            ymax = ~.fitted + 1.96 * .se.fit,
            line = list(color = 'rgba(7, 164, 181, 0.05)'),
            fillcolor = 'rgba(7, 164, 181, 0.2)',
            name = "Standard Error")

【问题讨论】:

    标签: r function plotly lm broom


    【解决方案1】:

    其中一种可能性是将数据框和列名都传递给您的函数,例如

    library(plotly)
    
    my_plot <- function(data, x, y, ...) {
      require(broom)
      plot_ly(y = data[[y]], x = data[[x]], showlegend = FALSE, ...) %>%
        add_markers(y = data[[y]]) %>%
        add_lines(y = ~fitted(lm(data[[y]] ~ data[[x]]))) %>%
        add_ribbons(data = augment(lm(data[[y]] ~ data[[x]])),
                    ymin = ~.fitted - 1.96 * .se.fit,
                    ymax = ~.fitted + 1.96 * .se.fit,
                    line = list(color = 'rgba(7, 164, 181, 0.05)'),
                    fillcolor = 'rgba(7, 164, 181, 0.2)',
                    name = "Standard Error")
    }
    my_plot(data = mtcars, y = 'mpg', x = 'disp')
    

    或者只是列本身。

    library(plotly)
    
    my_plot <- function(x, y, ...) {
      require(broom)
      plot_ly(y = y, x = x, showlegend = FALSE, ...) %>%
        add_markers(y = y) %>%
        add_lines(y = ~fitted(lm(y ~ x))) %>%
        add_ribbons(data = augment(lm(y ~ x)),
                    ymin = ~.fitted - 1.96 * .se.fit,
                    ymax = ~.fitted + 1.96 * .se.fit,
                    line = list(color = 'rgba(7, 164, 181, 0.05)'),
                    fillcolor = 'rgba(7, 164, 181, 0.2)',
                    name = "Standard Error")
    }
    my_plot(y = mtcars$mpg, x = mtcars$disp)
    

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 2012-05-06
      • 2017-05-08
      • 2021-01-09
      • 2018-04-17
      • 1970-01-01
      • 2015-11-20
      • 2020-06-13
      相关资源
      最近更新 更多