简体   繁体   English

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

[英]Adding a weighted least squares trendline in ggplot2

I am preparing a plot using ggplot2, and I want to add a trendline that is based on a weighted least squares estimation. 我正在使用ggplot2准备一个情节,我想添加一个基于加权最小二乘估计的趋势线。

In base graphics this can be done by sending a WLS model to abline : 在基本图形中,这可以通过发送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)

in ggplot2 I set the argument weight in geom_smooth but nothing changes: 在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)

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. 不要将重量名称放在引号中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM