繁体   English   中英

r scatter plot 和回归线问题

[英]r scatter plot and regression line issues

我正在尝试为此图创建回归线,为什么下面的代码不起作用?

这样做之后,我需要创建一个带有标签的自定义图例:名为“实际”的黑点和名为“预测”的蓝色短划线(对应于线)

library(ggplot2)
d1 <- c(20,30,40,50,60,70)
d2 <- c(23, 32,41,53,60,69)

df <- data.frame(d1, d2)


ggplot(df,aes(x=d1,y=d2)) +
  geom_point(size=1.1)+
  geom_smooth(method = "glm", 
              method.args = list(family = "binomial"), 
              se = FALSE)  

由于图表中因变量的值范围不在 0 和 1 之间,因此在geom_smooth()中,族不能是二项式,如ggplot()的错误消息所述:

`geom_smooth()` using formula 'y ~ x'
Warning message:
Computation failed in `stat_smooth()`:
y values must be 0 <= y <= 1 

如果我们使用family=的默认值,则会打印回归线。

library(ggplot2)
d1 <- c(20,30,40,50,60,70)
d2 <- c(23, 32,41,53,60,69)

df <- data.frame(d1, d2)


ggplot(df,aes(x=d1,y=d2)) +
     geom_point(size=1.1)+
     geom_smooth(method = "glm", 
                 se = FALSE)  

在此处输入图像描述

注释 plot 的一种方法是添加回归线和 R^2 信息。 我们可以使用ggpubr package 及其stat_regline_equation() function 来做到这一点。

library(ggpubr)
ggscatter(df, x = "d1", y = "d2", add = "reg.line",
          add.params = list(color = "blue", fill = "lightgray")) +
     stat_cor(label.x = 3, label.y = 70) +
     stat_regline_equation(label.x = 3, label.y = 66)

 

...和 output:

在此处输入图像描述

暂无
暂无

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

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