我试着用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()我得到的是:

这就是我想得到的:

我的密码怎么了?
发布于 2018-10-13 04:34:26
我用:
geom_quantile(quantiles = 0.5)把这个弄到手。毕竟不需要预测。
发布于 2018-10-13 03:43:45
我认为,如果您使用的是错误的geom,那么使用geom_smooth会有更多的运气。这提供了一些选项,但您可能需要+geom_smooth(method="lm")。即使在安装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_smooth(method="lm") +
theme_minimal()另外,如果您想轻松地重命名您的图例,只需在labs()参数中添加colour =“”即可。
https://stackoverflow.com/questions/52789235
复制相似问题