简体   繁体   English

带有循环数据的Rscript图

[英]Rscript plot with loop over data

I apologies in advance if this question has already been answered but I have not been able to find what I need. 如果这个问题已经得到解答,我提前道歉,但我找不到我需要的东西。 I want to plot some results from files named data1.dat, data2.dat, ... I manage to import the data with a loop but I am unable to plot the results using a loop. 我想从名为data1.dat,data2.dat,...的文件中绘制一些结果。我设法用循环导入数据,但我无法使用循环绘制结果。 Only the results from the first data is ploted. 仅绘制第一个数据的结果。 Below is the script that I used: 以下是我使用的脚本:

for(i in 1:3){
  assign( paste("data", i, sep=""),
  read.table(file=paste(paste("data", i, sep=""),"_lambda.dat",sep=""),head=TRUE, sep=" "))
}

#plot
for(i in 1:3){
  if(i==1){
    plot(data.frame(data1[1], data1[2]), xlim=c(-0.2, -7), ylim=c(0.31, 0.35), yaxt="n", type="l", xlab="", ylab="",lty="solid", col="red2", lwd=4, axes=TRUE)
  } else { 
    lines(data.frame(paste("data", i, "[1]", sep=""), paste("data", i, "[2]", sep="")) ,lty="twodash", col="deepskyblue4", lwd=4)
  }
}

The problem is related to the part after the "else". 问题与“其他”之后的部分有关。 The data is not ploted and I don't get any error message either. 数据没有绘制,我也没有收到任何错误信息。

Thanks for the help, 谢谢您的帮助,

There is hardly ever a meaningful use for assign . assign几乎没有任何有意义的用途。 What you do above could be much more easily be done using lists: 使用列表可以更轻松地完成上述操作:

# Read the data by inserting each data.frame into our list.
data <- list()
for (i in 1:3) {
    data[[i]] <- data.frame(runif(10), rnorm(10)) # Replace with your call to read.table.
}

# As we are lazy, we can pre-save our styles and just use them later
ltys <- c('solid', 'twodash', 'twodash')
cols <- c('red', 'deepskyblue4', 'deepskyblue4')

# Creating the empty plot first allows us to do everything else with lines. Note that there are a ton of different ways to archive this. 
plot(NA, xlim=c(0,1), ylim=c(-2,2))
# And now we just may access the data by using the lists. 
for (i in 1:length(data)) {
    d <- data[[i]]
    lines(d[,1], d[,2], lty=ltys[i], col=cols[i], lwd=4)
}

If we are aiming for elegance, we could even use a totally different approach: 如果我们的目标是优雅,我们甚至可以采用完全不同的方法:

# This time we will read all data into one data.frame.
# This requires the data to be of the same form, however. 
data <- data.frame()
for (i in 1:3) {
    d <- data.frame(x=runif(10), y=rnorm(10))
    # Here is the trick: We add the index i (or a file name, or something totally different) as a separate column.
    data <- rbind(data, cbind(d, group=i))
}
# This is just to ensure that our group column is a factor.
data$group <- as.factor(data$group)

# Now, plotting could be done by various ways. Personally, I like the elegance of ggplot2. 
library(ggplot2)
qplot(x,y,data=data, col=group, geom="line")

This is because you are effectively only using data1 . 这是因为您实际上只使用data1

The first loop can be expanded to 第一个循环可以扩展为

data1 = read.table('data1_lambda.dat', ...)
data2 = read.table('data2_lambda.dat', ...)
data3 = read.table('data3_lambda.dat', ...)

whereas your second loop is a bit buggy and the cause of the fault. 而你的第二个循环是有点马车和故障的原因。 If I expand the loop, what happens is: 如果我扩展循环,会发生什么:

plot(data.frame(data1[1], data1[2]), ...)
lines(data.frame('data2[1]', 'data2[2]', ...)
lines(data.frame('data3[1]', 'data3[2]', ...)

ie what you think is fetching data2 and data3 , you are really only using the strings 'data2' and 'data3' (note the quotation marks). 即你认为是获取data2data3 ,你实际上只使用字符串'data2''data3' (注意引号)。

Without assuming too much of your data and restructuring you entire code, this should do the trick (for the second loop). 如果不假设您的数据太多并重构整个代码,这应该可以解决问题(对于第二个循环)。 Since you only have three data-files, looping seems a bit extensive contra the problems they are introducing. 由于您只有三个数据文件,因此循环似乎与它们引入的问题相比有点广泛。

plot(data.frame(data1[1], data1[2]), xlim=c(-0.2, -7), ylim=c(0.31, 0.35), yaxt="n", type="l", xlab="", ylab="",lty="solid", col="red2", lwd=4, axes=TRUE)
lines(data.frame(data2[1], data2[2]) ,lty="twodash", col="deepskyblue4", lwd=4)
lines(data.frame(data3[1], data3[2]) ,lty="twodash", col="deepskyblue4", lwd=4)

If you insist on looping, we could do: 如果你坚持循环,我们可以这样做:

for(i in 1:3){
  if(i==1){
    plot(data.frame(data1[1], data1[2]), xlim=c(-0.2, -7), ylim=c(0.31, 0.35), yaxt="n", type="l", xlab="", ylab="",lty="solid", col="red2", lwd=4, axes=TRUE)
  } else { 
    dat <- get(paste('data', i, sep=''))
    lines(data.frame(dat[1], dat[2]) ,lty="twodash", col="deepskyblue4", lwd=4)
  }
}

To further comment your code, in your read.table call, you have two nested paste calls, which in this case is unnecessary. 要进一步评论您的代码,在read.table调用中,您有两个嵌套的paste调用,在这种情况下是不必要的。

paste(paste("data", i, sep=""),"_lambda.dat",sep="") # could be done with paste(paste("data", i, "_lambda.dat",sep="") paste(paste(“data”,i,sep =“”),“_ lambda.dat”,sep =“”)#可以用paste完成(paste(“data”,i,“_ lambda.dat”,sep = “”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM