【发布时间】:2019-11-24 22:03:34
【问题描述】:
我有 3 个变量。 x = 年龄,y = 消费分数和性别(女性/男性)。我想查看每种性别的 b/w y 和 x 关系。我有一个散点图,其中有两条回归线,一条用于女性,一条用于男性:公式 = y ~ x,方法 =“lm”。我想把这两个回归方程放在图上,但是默认位置(左上角)的方程很难读。所以我需要将两个回归方程的位置更改为右上角的区域。但是当我尝试这两个方程时,它们是相互重叠的,所以它们再次难以阅读。请帮忙!谢谢!
x <- df$age
y <- df$spending_score
formula <- y ~ x
k <- ggplot(df, aes(age, spending_score, color = gender)) +
geom_point() +
labs(x = "Age", y = "Spending score", title = "Spending score vs. age") +
stat_smooth(aes(fill = gender, color = gender), method = "lm", formula = formula) +
stat_regline_equation(
label.x = 50, label.y = 80,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~~")),
formula = formula
) +
theme_classic2() +
theme(plot.title = element_text(hjust = 0.5))
ggpar(k, palette = "jco")
【问题讨论】:
标签: r ggplot2 linear-regression