【问题标题】:Labeling convergence plots in R在 R 中标记收敛图
【发布时间】:2015-11-04 09:51:47
【问题描述】:

我正在为 R 中的 RDS 数据集创建收敛图,并希望标记这些图。现在,我的 x 轴是“观察次数”,我的 y 轴是 RDS 估计值,图本身标记为“clientcondom=1 的收敛图”。有没有办法改变这个?见以下代码:

convergence.plot(site[[1]], 'clientcondom', est.func=RDS.I.estimates) convergence.plot(site[[2]], 'clientcondom', est.func=RDS.I.estimates) convergence.plot(site[[3]], 'clientcondom', est.func=RDS.I.estimates)

另外,有没有办法将这些地块组合成一个地块——我在这里有三个站点,最好将它们组合起来并并排查看。非常感谢您的回复!

苏门答腊

【问题讨论】:

  • convergence.plot 来自哪里?
  • @Heroka,据我了解,收敛图来自RDS数据框,库是RDS。我对 R 很陌生,所以自己弄清楚这些 - 任何建议都将不胜感激,谢谢!
  • 我在convergence.plot的源代码中为你做了一些支持,但我没有看到任何不涉及自己修改代码或重新使用部分代码的解决方案创建自己的情节...

标签: r plot convergence


【解决方案1】:

让我们从您的第二个问题开始:如果您的数据在一个 rds.data.frame 中,您可以轻松地将您的三个网站绘制在同一个图表上。

首先,让我们像你一样创建一个列表

set.seed(1)
site1_df <-as.rds.data.frame(data.frame(id=1:10,recruiter.id=c("seed",5,7,3,8,2,10,9,1,6),network.size.variable=c(5,4,8,9,1,2,6,7,10,3),site1=as.factor(sample(c("blue", "red"), 10, replace = TRUE))))
site2_df <-as.rds.data.frame(data.frame(id=1:10,recruiter.id=c("seed",5,7,3,8,2,10,9,1,6),network.size.variable=c(5,4,8,9,1,2,6,7,10,3),site2=as.factor(sample(c("blue", "red"), 10, replace = TRUE))))
site3_df <-as.rds.data.frame(data.frame(id=1:10,recruiter.id=c("seed",5,7,3,8,2,10,9,1,6),network.size.variable=c(5,4,8,9,1,2,6,7,10,3),site3=as.factor(sample(c("blue", "red"), 10, replace = TRUE))))
sites_list <-list(site1_df,site2_df,site3_df)

然后将列表缩减为单个rds.data.frame

sites_df <-Reduce(function(...) merge(..., all=T), sites_list)
sites_rds <-as.rds.data.frame(sites_df)

然后您可以在同一个图表中绘制所有网站:

convergence.plot(sites_rds,c("site1","site2","site3"), est.func=RDS.I.estimates)

对于您的第一个问题,您必须基于convergence.plot 创建自己的convergence.plot2 函数。下面的convergence.plot2 函数为ylab 放置“Y 轴估计”,为xlab 放置“X 轴观察次数”和 “您的 (site1,2,3) 的标题收敛图”为您的 title。根据自己的喜好更改它们。确保更改所有出现。

convergence.plot2 <-function (rds.data, outcome.variable, est.func = RDS.II.estimates,
    as.factor = FALSE, ...)
{
    if (as.factor) {
        for (o in outcome.variable) {
            rds.data[[o]] <- as.factor(rds.data[[o]])
        }
    }
    f <- function(v) cumulative.estimate(rds.data, v, est.func,
        ...)
    ests <- lapply(outcome.variable, f)
    make.plot <- function(i) {
        Var1 <- Var2 <- value <- NULL
        e <- ests[[i]]
        nm <- outcome.variable[i]
        if (ncol(e) == 2) {
            e1 <- e[, 2, drop = FALSE]
            attr(e1, "n") <- attr(e, "n")
            e <- e1
            nm <- paste0(outcome.variable[i], "=", colnames(e)[1])
            rds.data[[outcome.variable[i]]] <- as.factor(rds.data[[outcome.variable[i]]])
        }
        if (ncol(e) > 1) {
            rownames(e) <- attr(e, "n")
            dat <- melt(e)
            datl <- melt(e[nrow(e), , drop = FALSE])
            p <- ggplot(dat) + geom_line(aes(x = Var1, color = as.factor(Var2),
                y = value)) + scale_color_hue(nm) + ylab("Y-axis Estimate") +
                xlab("X-Axis # of Observations") + scale_y_continuous(limits = c(0,
                1)) + theme_bw()
            p <- p + geom_hline(data = datl, aes(yintercept = value,
                color = as.factor(Var2)), linetype = 2, alpha = 0.5)
            p
        }
        else {
            dat <- data.frame(value = e[, 1], Var1 = attr(e,
                "n"))
            datl <- dat[nrow(dat), , drop = FALSE]
            v <- rds.data[[outcome.variable[i]]]
            rng <- if (!is.numeric(v))
                c(0, 1)
            else range(v, na.rm = TRUE)
            p <- ggplot(dat) + geom_line(aes(x = Var1, y = value)) +
                ylab(paste("Estimated", nm)) + xlab("# of Observations") +
                scale_y_continuous(limits = rng) + theme_bw()
            p <- p + geom_hline(data = datl, aes(yintercept = value),
                linetype = 2, alpha = 0.5)
            p
        }
        return(p + ggtitle(paste("Your title Convergence plot of", nm)))
    }
    plots <- lapply(1:length(outcome.variable), make.plot)
    do.call(.grid.arrange_RDS, plots)
}

然后将这个新函数添加到RDS 环境中很重要。 RDS 使用的功能只能在自己的环境中找到。

environment(convergence.plot2) <- asNamespace('RDS')

调用convergence.plot2:

convergence.plot2(sites_rds,c("site1","site2","site3"), est.func=RDS.I.estimates)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 2020-04-29
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    相关资源
    最近更新 更多