在ggplot2中添加加权最小二乘趋势线 [英] Adding a weighted least squares trendline in ggplot2
本文介绍了在ggplot2中添加加权最小二乘趋势线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用ggplot2准备图,并且我想添加基于加权最小二乘估计的趋势线.
I am preparing a plot using ggplot2, and I want to add a trendline that is based on a weighted least squares estimation.
在基本图形中,这可以通过将WLS模型发送到abline
来完成:
In base graphics this can be done by sending a WLS model to abline
:
mod0 <- lm(ds$dMNP~ds$MNP)
mod1 <- lm(ds$dMNP~ds$MNP, weights = ds$Asset)
symbols(ds$dMNP~ds$MNP, circles=ds$r, inches=0.35)
#abline(mod0)
abline(mod1)
ggplot2中的
我在geom_smooth
中设置了参数weight
,但没有任何变化:
in ggplot2 I set the argument weight
in geom_smooth
but nothing changes:
ggplot(ds, aes(x=MNP, y=dMNP, size=Asset) +
geom_point(shape=21) +
geom_smooth(method = "lm", weight="Asset", color="black", show.legend = FALSE)
这给了我与...相同的情节
this gives me the same plot as
ggplot(ds, aes(x=MNP, y=dMNP, size=Asset) +
geom_point(shape=21) +
geom_smooth(method = "lm", color="black", show.legend = FALSE)
推荐答案
我来晚了,但是为了后代和清晰起见,这里是完整的解决方案:
I'm late, but for posterity and clarity, here is the full solution:
ggplot(ds, aes(x = MNP, y = dMNP, size = Asset)) +
geom_point(shape = 21) +
geom_smooth(method = "lm", mapping = aes(weight = Asset),
color = "black", show.legend = FALSE)
不要将重量名称用引号引起来.
Don't put the weight name in quotes.
这篇关于在ggplot2中添加加权最小二乘趋势线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文