Reputation: 11
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)
Upvotes: 0
Views: 605
Reputation: 10845
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:
Upvotes: 1