【问题标题】:How to plot weighted loess smoothing in ggplot2? [duplicate]如何在ggplot2中绘制加权黄土平滑? [复制]
【发布时间】:2014-03-26 15:01:03
【问题描述】:

如何添加将另一列视为权重的 loess-smoothing?

假设我有以下data.frame:

    library(ggplot2)

    df <- data.frame(x=seq(1:21))
    df$y <- df$x*0.3 + 10
    df$weight <- 10

    df[6,] <- c(6, 0.1, 1)
    df[7,] <- c(7, 0.1, 1)

    df[13,] <- c(13, 0.1, 1)
    df[14,] <- c(14, 0.1, 1)

    df[20,] <- c(20, 0.1, 1)
    df[21,] <- c(21, 0.1, 1)

    ggplot(data=df, aes(x=x, y=y, size=weight)) + geom_point() + geom_smooth(method=loess, legend=FALSE)

绘制一个 loess-smoothing 会产生以下结果:

但我想使用列权重,使得 loess 与权重为 10 的每个点都存在 10 倍一样:

    df2 <- subset(df, !(x %in% c(6,7,13,14,20,21)))
    df2$weight <- 1

    df3 <- df2
    for(i in seq(1:9)){
      df3 <- rbind(df3, df2)
    }
    df3 <- rbind(df3, c(6, 0.1, 1))
    df3 <- rbind(df3, c(7, 0.1, 1))

    df3 <- rbind(df3, c(13, 0.1, 1))
    df3 <- rbind(df3, c(14, 0.1, 1))

    df3 <- rbind(df3, c(20, 0.1, 1))
    df3 <- rbind(df3, c(21, 0.1, 1))

    ggplot(data=df3, aes(x=x, y=y)) + geom_point() + geom_smooth(method=loess, legend=FALSE) 
    # or to demonstrate
    ggplot(data=df3, aes(x=x, y=y)) + geom_jitter() + geom_smooth(method=loess, legend=FALSE)

我找到了函数loess的参数权重。但我不知道如何从 geom_smooth() 调用它。

【问题讨论】:

标签: r ggplot2 smooth loess


【解决方案1】:

我想我只需要在 aes 中添加另一个参数:

  ggplot(data=df, aes(x=x, y=y, size=weight, weight=weight)) + geom_point() + geom_smooth(method=loess, legend=FALSE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2020-10-02
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多