在ggplot2中添加加权最小二乘趋势线

Maj*_*ian 7 r ggplot2 weighted trendline

我正在使用ggplot2准备一个情节,我想添加一个基于加权最小二乘估计的趋势线.

在基础图形中,这可以通过将WLS模型发送到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)
Run Code Online (Sandbox Code Playgroud)

在GGPLOT2我设置的参数weightgeom_smooth,但没有改变:

ggplot(ds, aes(x=MNP, y=dMNP, size=Asset) + 
  geom_point(shape=21) +
  geom_smooth(method = "lm", weight="Asset", color="black", show.legend = FALSE)
Run Code Online (Sandbox Code Playgroud)

这给了我同样的情节

ggplot(ds, aes(x=MNP, y=dMNP, size=Asset) + 
  geom_point(shape=21) +
  geom_smooth(method = "lm", color="black", show.legend = FALSE)
Run Code Online (Sandbox Code Playgroud)

ard*_*aar 8

我迟到了,但为了后人和清晰,这里是完整的解决方案:

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)
Run Code Online (Sandbox Code Playgroud)

不要将重量名称放在引号中.