Custom R function around plot_ly() with fitted(lm(y~x)) using add_lines()

三世轮回 提交于 2019-12-12 05:59:15

问题


I want to write a custom function around plot_ly() in R. That way, I can make a series of scatterplots with the same formatting and style, but not duplicate code. I used this page as a guide. This code reproduces the error:

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)

The problem line is:

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

I tried using as.formula(), but the error message is similar.

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

Here is the error message:

Error in model.frame.default(formula = y ~ x, data = mtcars, drop.unused.levels = TRUE) : object is not a matrix

The code works when it's not a function:

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")

回答1:


One of the possibilities would be to pass both the dataframe and the column names to your function, e.g.

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')

or just the columns themselves.

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)


来源:https://stackoverflow.com/questions/42335421/custom-r-function-around-plot-ly-with-fittedlmyx-using-add-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!