[英]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()
:由于图表中因变量的值范围不在 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
If we use the default value for family=
, the regression line prints.如果我们使用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)
One way to annotate the plot would be to add the regression line and R^2 information.注释 plot 的一种方法是添加回归线和 R^2 信息。 We can do this with the ggpubr
package and its stat_regline_equation()
function.我们可以使用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)
...and the output: ...和 output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.