我想在图形中显示三个线性拟合。在图例中,我想显示各个组的标签旁边的相关系数。理想情况下,我将在标签中的实际系数前使用希腊语beta和=而不是“ rank corr:”。有谁知道如何做到这一点?非常感谢。
n <- 100
x <- rnorm(n)
y <- rnorm(n, mean = x)
grp <- sample(LETTERS[1:3], replace = TRUE, size = n)
# Do linear regressions for each group
lin_regs <- lapply(unique(grp), function(g) lm(y ~ x, subset = g==grp))
b1 <- format(summary(lin_regs[[1]])$coefficients[2, 1], digits=3)
b2 <- format(summary(lin_regs[[2]])$coefficients[2, 1], digits=3)
b3 <- format(summary(lin_regs[[3]])$coefficients[2, 1], digits=3)
plot1 <- sprintf("Basic, rank corr: %s", b1)
plot2 <- sprintf("Intermediate, rank corr: %s", b2)
plot3 <- sprintf("High, rank corr: %s", b3)
# Plot lines
plot(x, y, type = "n") # Initialize plot
for (i in seq_along(lin_regs)) {
abline(lin_regs[[i]], col = i) # Plot each linear reg. with abline()
}
legend('bottom', legend = c(plot1,plot2,plot3),
col=c('black', 'red', 'green'), lty=1)