我有一个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)可以在父列表中循环处理并保存对其的更改,而不是创建新的数据帧。
帮助表示赞赏。
答案 0 :(得分: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()