使用add_lines()函数对plot_ly()进行自定义R函数的拟合(lm(y〜x)) [英] Custom R function around plot_ly() with fitted(lm(y~x)) using add_lines()

查看:727
本文介绍了使用add_lines()函数对plot_ly()进行自定义R函数的拟合(lm(y〜x))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中的plot_ly()周围写一个自定义函数。这样,​​我可以制作一系列具有相同格式和样式的散点图,但不会重复代码。 我用这页作为指南。代码重现错误:

pre $ library $($)
my_plot< - function(x,y,... ){
require(broom)
plot_ly(data = mtcars,y = y,x = x,showlegend = FALSE,...)%>%
add_markers(y = y) %>%
add_lines(y =〜拟合(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, ),
fillcolor ='rgba(7,164,181,0.2)',
name =Standard Error)
}
my_plot(y = 〜mpg,x =〜disp)

问题行是:

  add_lines(y =〜拟合(lm(y〜x)))%>%

我尝试使用as.f ormula(),但错误消息是类似的。

  add_lines(y =〜拟合(lm(as.formula(y 〜x)))%>%

以下是错误消息:


在model.frame.default中的错误(公式= y〜x,data = mtcars,drop.unused.levels = TRUE):
object is不是矩阵


代码在不是函数时有效:

 library(plotly)
library(broom)
plot_ly(data = mtcars,y =〜mpg,x =〜disp,showlegend = FALSE)%>%
add_markers(y =〜mpg)%>%
add_lines(y =〜拟合(lm(mpg_dis)))%>%
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)


解决方案

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

  library(plotly)

my_plot< - function(data,x, y,...){
require(扫帚)
plot_ly(y = data [[y]],x = data [[x]],showlegend = FALSE,...)%> %
add_markers(y = data [[y]])%>%
add_lines(y =〜拟合(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, ),
name =Standard Error)
}
my_plot(data = mtcars,y ='mpg',x ='disp')

或者只是列自身。

 库(plotly )

my_plot< - 函数(x,y,...){
require(扫帚)
plot_ly(y = y,x = x,showlegend = FALSE, ...)%>%
add_markers(y = y)%>%
add_lines(y =〜拟合(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)


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

解决方案

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)

这篇关于使用add_lines()函数对plot_ly()进行自定义R函数的拟合(lm(y〜x))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆