【问题标题】:Adding a weighted least squares trendline in ggplot2在ggplot2中添加加权最小二乘趋势线
【发布时间】:2017-02-28 11:08:08
【问题描述】:

我正在使用 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)

在 ggplot2 中,我在 geom_smooth 中设置了参数 weight,但没有任何变化:

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

这给了我与

相同的情节
ggplot(ds, aes(x=MNP, y=dMNP, size=Asset) + 
  geom_point(shape=21) +
  geom_smooth(method = "lm", color="black", show.legend = FALSE)

【问题讨论】:

  • 您能否将其调整为内置数据集以实现重现性?
  • 你可以把weight放在aes中,geom_smooth会用到

标签: r ggplot2 weighted trendline


【解决方案1】:

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

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)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2016-08-06
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多