1

我有一个包含 1000 个数据框的列表(每个都有 4 个同名的列)。我想绘制绘图并以交互方式处理它们。以下是示例(单个列表有 3 个数据框)。

# dummy data 
c1 <- rnorm (40, 0.1, 0.02); c2 <- rnorm (40, 0.3, 0.01)
c3 <- rnorm (40, 0.5, 0.01); c4 <- rnorm (40, 0.7, 0.01)
c5 <- rnorm (40, 0.9, 0.03)
Yv <- 0.3 + rnorm (200, 0.05, 0.05)

 # frist dataframe 
    var1 <- data.frame (idnames = paste ("ID", 1:200, sep = ""), Theta = round (c(c1, c2, c3, c4, c5), 2), R = round (Yv, 2),     cltr = c(rep(1:5, each = 40)))

# same dummy data filled in second dataframe 
var3 <- data.frame (idnames = paste ("ID", 1:200, sep = ""), Theta = round (c(c1, c2, c3, c4, c5), 2), R = round (Yv, 2),     cltr = c(rep(1:5, each = 40)))

    # same dummy data filled in third dataframe
    var_5 <- data.frame (idnames = paste ("ID", 1:200, sep = ""), Theta = round (c(c1, c2, c3, c4, c5), 2), R = round (Yv, 2),     cltr = c(rep(1:5, each = 40)))

 # the list (the original list has >1000 dataframes)
    mylist <- list (var1 = var1, var3 = var3, var_5 = var_5)

我想为每个数据框创建相似的图,但我需要一个一个地工作并保存并移至下一个图

# plot for var1
myd1 <- data.frame (mylist[1])
names (myd1) <- c("idnames", "Theta", "R", "cltr")
plot(myd1$Theta, myd1$R, col=myd1$cltr+1, pch = 19, main = names (mylist[1]))
exclude <- identify(myd1$Theta, myd1$R)
## left click on the points you want to exclude (right click to stop/finish)
myd1$cltr1 <- as.numeric ( myd1$cltr)+ 1
myd1$cltr1[exclude] <-  1
plot(myd1$Theta, myd1$R, col=myd1$cltr1, pch = 19, main = names (mylist[1]))

在此处输入图像描述

我可以对列表中的其他数据框执行相同的操作,例如 2:

myd2 <- data.frame (mylist[2])
names (myd2) <- c("idnames", "Theta", "R", "cltr")
plot(myd2$Theta, myd1$R, col=myd2$cltr+1, pch = 19, main = names (mylist[2]))
exclude <- identify(myd2$Theta, myd2$R)
## left click on the points you want to exclude (right click to stop/finish)
myd2$cltr1 <- as.numeric ( myd2$cltr)+ 1
myd2$cltr1[exclude] <-  1
plot(myd2$Theta, myd1$R, col=myd2$cltr1, pch = 19, main = names (mylist[2]))

我有一些问题:

(1)我想循环这个过程,一个图一次弹出我们,当编辑完成时,图关闭,下一个数据框的新图弹出工作等等,并继续到列表中的最后一个数据框。

(2) 是否可以在父列表中循环进程并保存对它的更改,而不是创建新的数据框。

帮助表示赞赏。

4

1 回答 1

1

我不会详细说明你想在每个绘图上做什么,但我会构建一个函数来执行你想要做的任何绘图和保存,包括一条scan等待用户输入的行,然后触发循环的下一次迭代。

plotwait <- function(){ # could pass arguments for saving, etc.
    x <- rnorm(100,0,1)
    y <- rnorm(100,0,1)
    plot(y~x)
    # other stuff you want to do to plot here
    z <- scan(nlines=1,quiet=TRUE) # wait for any input
}

for(i in 1:1000)
    plotwait()
于 2013-05-12T16:18:20.747 回答