简体   繁体   English

在GGPlot上绘制GLM回归方程和Rsquared

[英]Plot GLM Regression Equation and Rsquared on GGPlot

I have a problem like this: 我有这样的问题:

Sample data: 样本数据:

library(tidyverse)
library(ggpubr)
a <- mtcars
reorganized <- a %>% gather (-mpg, key = "var", value = "value")

g <- ggplot(reorganized, aes(y = value, x = var)) +
  stat_smooth(method="glm", se=TRUE, fill=NA, 
              method.args = list(family = "binomial"), fullrange = F) +
  geom_smooth(method="glm", fill='red',
              method.args = list(family = "binomial")) +
  stat_regline_equation(
    aes(label =  paste(..eq.label.., ..adj.rr.label.., sep = "~~~~"))
  )+
  geom_point(aes(), alpha=2/10, shape=21, fill="blue", colour="black", size=0.2) +
  facet_wrap(~var, nrow=1)+
  theme_bw()

The data is the reorganized dataframe, with 3 columns: mpg, var, value 数据是reorganized数据框,有3列:mpg,var,value

Using this code, I want plot facet with logistic regression line, equation with scatter plot. 使用此代码,我想要使用逻辑回归线的绘图方面,使用散点图的方程。 However, the formula appears incorrect, they only has the form of y = b + ax , and sometimes the Radj is out of the plot 但是,公式显示不正确,它们只有y = b + ax的形式,有时Radj不在图中 在此输入图像描述

How can I draw the correct formula of logistic regression in this figure? 如何在此图中绘制逻辑回归的正确公式?

It seems you are trying to use the glm method for non-categorical variables. 您似乎正在尝试将glm方法用于非分类变量。 This won't work. 这不行。 Instead, you should use either "loess" or "lm" methods. 相反,您应该使用“黄土”或“lm”方法。 Also, to insert correctly the equation inside each facet you can control the position and the size of the equation. 此外,要在每个方面内正确插入方程,您可以控制方程的位置和大小。 The code would be the following (geom_smooth defaults to "loess"): 代码如下(geom_smooth默认为“loess”):

ggplot(reorganized, aes(y = value, x = mpg)) +
  geom_smooth(se=TRUE, fill=NA, method.args=list(family="gaussian"), fullrange = F) +
  stat_regline_equation( aes(label =  paste(..eq.label.., ..adj.rr.label.., 
                                             sep = "~~~~")),
                         label.x.npc = "left", label.y.npc = "top", size = 2)+
  geom_point(aes(), alpha=2/10, shape=21, fill="blue", colour="black", size=0.2) +
  facet_wrap(~var, nrow=2)+
  theme_bw()

It should look like this 它看起来应该是这样的

By default, geom_smooth is set to the loess method with the formula y ~ x. 默认情况下,geom_smooth设置为黄土方法,公式为y~x。 That is why you only have y and x on the equations. 这就是你在方程式上只有y和x的原因。 If you want a different formula you should look for how to customize formulas in geom_smooth. 如果您想要一个不同的公式,您应该寻找如何在geom_smooth中自定义公式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM