我正在嘗試繪制 50 條模擬回歸線。我可以在基礎 R 中進行繪圖,但我正在努力做到這一點ggplot2
模擬資料:
library(tidyverse)
set.seed(5)
priors <- tibble(a = rnorm(n = 50, mean = 0, sd = 1),
bN = rnorm(n = 50, mean = 0, sd = 1))
使用基數 R 的所需輸出:
plot( NULL , xlim=c(-2,2) , ylim=c(-2,2) )
for(i in 1:50) curve(priors$a[i] priors$bN[i]*x,
from = -2, to = 2, add = TRUE)

我怎樣才能做同樣的事情ggplot2呢?
uj5u.com熱心網友回復:

priors %>%
ggplot()
geom_abline(aes(intercept = a, slope = bN))
xlim(-2,2)
ylim(-2,2)
theme_classic()
uj5u.com熱心網友回復:
geom_abline()添加一條具有指定截距和斜率的線。
gg0 <- ggplot() scale_x_continuous(limits=c(-2,2))
scale_y_continuous(limits=c(-2,2))
gg <- gg0
for (i in 1:50) {
gg <- gg geom_abline(intercept=priors$a[i],slope = priors$bN[i])
}
print(gg)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/435053.html
