Custom R function around plot_ly() with fitted(lm(y~x)) using add_lines()
问题 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