【问题标题】:Rscript plot with loop over data带有循环数据的 Rscript 绘图
【发布时间】:2013-05-25 13:36:19
【问题描述】:

如果这个问题已经得到解答,但我无法找到我需要的东西,我提前道歉。 我想从名为 data1.dat、data2.dat 的文件中绘制一些结果...我设法通过循环导入数据,但无法使用循环绘制结果。仅绘制第一个数据的结果。以下是我使用的脚本:

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)
  }
}

问题与“else”之后的部分有关。没有绘制数据,我也没有收到任何错误消息。

感谢您的帮助,

【问题讨论】:

标签: r for-loop


【解决方案1】:

assign 几乎没有任何有意义的用途。使用列表可以更轻松地完成上述操作:

# 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)
}

如果我们的目标是优雅,我们甚至可以使用完全不同的方法:

# 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")

【讨论】:

    【解决方案2】:

    这是因为您实际上只使用了data1。

    第一个循环可以扩展为

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

    而您的第二个循环有点错误并且是故障的原因。 如果我展开循环,会发生什么:

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

    即您认为获取data2 和data3,实际上您只使用字符串'data2' 和'data3'(注意引号)。

    无需假设您的数据过多并重组您的整个代码,这应该可以解决问题(对于第二个循环)。由于您只有三个数据文件,因此与它们引入的问题相比,循环似乎有点广泛。

    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)
    

    如果你坚持循环,我们可以这样做:

    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)
      }
    }
    

    为了进一步注释您的代码,在您的 read.table 调用中,您有两个嵌套的 paste 调用,在这种情况下是不必要的。

    paste(paste("data", i, sep=""),"_lambda.dat",sep="") # 可以用 粘贴(粘贴(“数据”,我,“_lambda.dat”,sep="")

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2012-03-24
      相关资源
      最近更新 更多