[英]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.