r - 保存 "summary(lm)"的结果以在 Power Bi 中使用

标签 r ggplot2 lm

是否可以以 PowerBI 中可用的格式保存摘要 (lm) 对象?

让我们说以下内容:

data <- mpg
lm <- lm(hwy ~ displ, data = mpg)
summary(lm)

输出:

Call:
lm(formula = hwy ~ displ, data = mpg)

Residuals:
    Min      1Q  Median      3Q     Max 
-7.1039 -2.1646 -0.2242  2.0589 15.0105 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  35.6977     0.7204   49.55   <2e-16 ***
displ        -3.5306     0.1945  -18.15   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.836 on 232 degrees of freedom
Multiple R-squared:  0.5868,    Adjusted R-squared:  0.585 
F-statistic: 329.5 on 1 and 232 DF,  p-value: < 2.2e-16

我希望将此信息保存为 gglpot2 对象或一般图片,以便可以在 Power BI 中显示。这样我们就可以使用它作为 Power BI 内快速回归的模板。这是因为 Power BI 只能显示导致“绘图”而不是文本的 R 代码。

我已经尝试过:

textplot(capture.output(summary(lm)))

但我首先得到这个错误:

>install.packages('textplot')
Warning in install.packages : 
    package ‘textplot’ is not available (for R version 3.5.3)

不幸的是,Power BI 不支持 textplot()。

编辑:澄清我不想绘制回归线或平面。我正在寻找一种方法,将“summary(lm)”的文本输出保存为可以在 Power BI 中显示的绘图对象。

最佳答案

尝试这样的事情:

fit <- lm(hwy ~ displ, data = mpg)
txt = capture.output(print(summary(fit)))

plot(NULL,xlim=c(-1,1),ylim=c(-1,1),xaxt="n",yaxt="n",bty="n",xlab="",ylab="")
text(x=0,y=0,paste(txt,collapse="\n"))

enter image description here

您可能需要考虑使用 stringr::str_pad 来使文本更漂亮..但这应该会给您带来一些有用的东西。

这就是将其放在 ggplot2 上的方式:

ggplot() + xlim(c(-1,1)) + ylim(c(-1,1)) + 
geom_text(aes(x=0,y=0,label=paste(txt,collapse="\n"))) + 
    theme_void()

enter image description here

关于r - 保存 "summary(lm)"的结果以在 Power Bi 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60951362/

相关文章:

r - ggplot2:向 map 添加基本海岸线

r - 具有 geom_smooth 的对数 y

r - 无法让aggregate()按组进行回归工作

r - 更新R中嵌套循环中的列表

r - 使用 R 的 lm(),公式对象应该作为字符传递吗?

r - 将整数转换为 base36

r - 在 R 中安装 Cairo 包的错误消息

r - 使用点将提示标签链接到系统发育树

r - geom_text 来自初始绘图区域之外的单独文件

r - 在两个不同的 ("grid.arranged") 图之间画线