简体   繁体   中英

r scatter plot and regression line issues

I am trying to create to regression line for this graph, why is the below code not working?

After doing so, I need to create a custom legend with labels: black dot named 'actual' and blue dash (corresponding to the line) named 'predicted'

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)  

Since the range of values for the dependent variable in the chart is not between 0 and 1, in geom_smooth() the family can't be binomial, as noted by the error message from ggplot() :

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

If we use the default value for family= , the regression line prints.

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)  

在此处输入图像描述

One way to annotate the plot would be to add the regression line and R^2 information. We can do this with the ggpubr package and its 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)

 

...and the output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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