r语言 - 在 ggplot2 中绘制线性回归



我有这个数据帧:

> head(data)
    sx   yd  sl
1   male 35  36350
2   male 22  35350
3   male 23  28200
4 female 27  26775
5   male 30  33696
6   male 21  28516
其中"sx"是性别,">

yd"是获得学位后的年限,"sl"是薪水。使用 ggplot 或绘图,我可以轻松绘制散点。

palette(c("pink", "blue"))
plot(data$yr, data$sl, col = factor(data$sx), xlab = "Years Since Earned Highest Degree", ylab = "Salary (dollars)", main = "Salary Increases with Experience", pch = 19)
legend("topleft", legend = unique(data$sx), col = c("blue", "pink"), pch=19)
library(ggplot2)
ggplot(data, aes(x=yd,y=sl)) + 
    geom_point(shape=21, aes(col=sx, bg=sx)) + 
    xlab("Years Since Earned Highest Degree") + 
    ylab("Salary (dollars)") + 
    ggtitle("Salary Increases with Experience") + 
    scale_color_discrete(guide=FALSE) + 
    labs(fill="sex")

但是,我也根据数据制作了一个线性模型:

mod<-lm(sl~sx*poly(yd,2),data)

而且我无法弄清楚如何将数据绘制到图表中。具体来说,我希望两条对应于男性和女性数据的线叠加在散点图上,并且进行颜色编码。我假设 R 有某种方法可以做到这一点,这样我就不必实际写出模型。无论是基本图还是 ggplot 答案都很好。谢谢。

编辑:

运行上面的 ggplot 与 geom_smooth(aes(col=sx), se = FALSE, method = "lm", formula = sl ~ sx * poly(yd, 2))

ggplot(data, aes(x=yd,y=sl)) + geom_point(shape=21, aes(col=sx, bg=sx)) + geom_smooth(aes(col=sx), se = FALSE, method = "lm", formula = sl ~ sx * poly(yd, 2)) + xlab("Years Since Earned Highest Degree") + ylab("Salary (dollars)") + ggtitle("Salary Increases with Experience") + scale_color_discrete(guide=FALSE)+ labs(fill="sex")

返回此错误:

Error in model.frame.default(formula = formula, data = data, weights = weight,  : 
  variable lengths differ (found for '(weights)')
Error in if (nrow(layer_data) == 0) return() : argument is of length zero
data = data.frame(sx = c("male", "male", "male", "female", "male", "male"),
              yr = c(35, 22, 23, 27, 30, 21),
              sl = c(36350, 35350, 28200, 26775, 33696, 28516))
ggplot(data, aes(x=yr,y=sl)) + 
  geom_point(shape=21, aes(col=sx, bg=sx)) + 
  geom_smooth(aes(color = sx), se = FALSE, method = "lm", formula = y ~ poly(x, 2)) + 
  xlab("Years Since Earned Highest Degree") + 
  ylab("Salary (dollars)") + 
  ggtitle("Salary Increases with Experience") +     
  scale_color_discrete(guide=FALSE)+ labs(fill="sex")

这是你想要的吗?如果您有更多女性数据,您应该获得个人适合。现在sum(data$sx == 'female')是1。没有办法有一个多项式拟合。
例如,尝试:

data = data.frame(sx = c("male", "male", "male", "female", "male", "male", "female", "female", "female"),
                  yr = c(35, 22, 23, 27, 30, 21, 25, 18, 29),
                  sl = c(36350, 35350, 28200, 26775, 33696, 28516, 27402, 31492, 23195))

这应该有效。

我找不到 ggplot 方法来做到这一点,所以这里是基本的绘图方法:

palette(c("pink", "blue"))
plot(data$yr, data$sl, col = factor(data$sx), xlab = "Years Since Earned Highest Degree", ylab = "Salary (dollars)", main = "Salary Increases with Experience", pch = 19)
legend("topleft", legend = unique(data$sx), col = c("blue", "pink"), pch=19)
lines(seq(0,25,0.1), predict.lm(quad, data.frame(yd = seq(0,25,0.1), sx = "female", stringsAsFactors = TRUE)),col="pink", lwd = 5)
lines(seq(0,25,0.1), predict.lm(quad, data.frame(yd = seq(0,25,0.1), sx = "male", stringsAsFactors = TRUE)),col="blue", lwd = 5)

对线路的两次调用是解决方案。如果有人有 ggplot 的方式来做到这一点,我会非常感激,因为 ggplot 看起来好多了。