首页 > 解决方案 > ggplot2中的中值回归线

问题描述

我正在尝试使用 ggplot 来拟合中值回归线。这使用了 quantreg 包的 rq() 函数。数据是来自 HSAUR3 包的云数据。这是我的代码:

seeding.no <- predict(rq(rainfall ~ sne, data = clouds, subset = seeding == "no"))
seeding.yes <- predict(rq(rainfall ~ sne, data = clouds, subset = seeding == "yes"))
clouds.predcit <- data.frame(Rainfall=clouds$rainfall, No=seeding.no, Yes=seeding.yes)

ggplot(clouds, aes(x=sne, y=rainfall, colour=clouds$seeding, shape=clouds$seeding)) + 
  geom_point(size=2) +
  labs(title="Rainfall vs S-Ne Criterion", x="S-Ne Criterion", y="Rainfall") +
  scale_color_manual(values=c("blue", "red")) + 
  scale_shape_manual(values=c(1,2)) +
  geom_line(color="blue",data=clouds.predcit, aes(x=No, y=Rainfall)) +
  geom_line(color="red",data=clouds.predcit, aes(x=Yes, y=Rainfall)) +
  theme_minimal()

这就是我得到的:

在此处输入图像描述

这就是我想要得到的:

在此处输入图像描述

我的代码有什么问题?

标签: rggplot2

解决方案


我用了:

geom_quantile(quantiles = 0.5)

并让它工作。毕竟不需要预测。


推荐阅读