0

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

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

苏门答腊

4

1 回答 1

0

让我们从您的第二个问题开始:如果您的数据在一个单一的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.plot2基于convergence.plot. 下面的convergence.plot2函数ylabxlab您的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)

在此处输入图像描述

于 2015-11-04T15:38:55.183 回答