我正在按照here的指示在每个组的预测变量和响应之间创建散点图
require(data.table)
require(ggplot2)
group.nobs <- 50
group.beta0 <- c(-1., 0., 1.)
group.beta1 <- c(0.8, 1.0, 1.2)
predictor <- rnorm(n = group.nobs)
response <- c(group.beta0[1] + group.beta1[1] * predictor + rnorm(sd = 0.1, n = group.nobs),
group.beta0[2] + group.beta1[2] * predictor + rnorm(sd = 0.1, n = group.nobs),
group.beta0[3] + group.beta1[3] * predictor + rnorm(sd = 0.1, n = group.nobs))
df <- data.table(group=c(rep(1,group.nobs),rep(2,group.nobs),rep(3,group.nobs)), x=predictor, y=response)
ggplot(df, aes(x=x, y=y, color=group)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
但仅绘制了1条回归线
我希望每组3条回归线
感谢您的帮助
答案 0 :(得分:1)
您只需要使用group
美观性将数据分组:
ggplot(df, aes(x=x, y=y, color=group, group = group)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)