【问题标题】:For-loop to dynamically change plot title用于动态更改绘图标题的 for 循环
【发布时间】:2021-09-02 06:25:55
【问题描述】:

我有 5 个变量想要在一个 pdf 中绘制和导出。但是,我正在运行的 for 循环遇到了一些问题,

parC <-list(unit = 100,labelx = "Time",labely = "Time",cols = "black",
            pcex = .01, pch = 1,las = 1,
            labax = seq(0,nrow(RP),100),
            labay = seq(0,nrow(RP),100))

pdf("filename.pdf", onefile=TRUE) 
for (i in RP_values){ # the values that are plotted
  for (j in name) { # name is a list of names, so that the title changes dynamically
  plotting(i, parC, j)
  }
}
dev.off()

RP_values = 绘制的值列表 name = 动态更改绘图标题的名称列表 plotting = crqa 包的 plotRP() 函数的调整版本。在这里,我为情节添加了一个主标题。

plotting() 函数的代码:

plotting <- function(RP, par, x){
  
  if (exists("par") == FALSE){ # we use some defaults
    ## default values
    unit  = 2; labelx = "Time"; labely = "Time" 
    cols  = "black"; pcex = .3; pch = 1; las = 0;
    labax = seq(0, nrow(RP), unit); labay = seq(0, nrow(RP), unit);
  } else { # we load the values that we desire
    for (v in 1:length(par)) assign(names(par)[v], par[[v]])
  }
  
  xdim   = nrow(RP)
  ydim   = ncol(RP)
  
  RP = matrix(as.numeric(RP), nrow = xdim, ncol = ydim) # transform it for plotting
  
  ind = which(RP == 1, arr.ind = T)
  
  tstamp = seq(0, xdim, unit)
  
  par(mar = c(5,5, 1, 3), font.axis = 2, cex.axis = 1,
      font.lab = 2, cex.lab = 1.2)
  
  plot(tstamp, tstamp, type = "n", xlab = "", ylab = "", xaxt = "n", yaxt = "n", main = x)
  matpoints(ind[,1], ind[,2],  cex = pcex, col = cols, pch = pch) 
  
  mtext(labelx, at = mean(tstamp), side = 1, line = 2.2, cex = 1.2, font = 2)
  mtext(labely, at = mean(tstamp), side = 2, line = 2.2, cex = 1.2, font = 2)
  
  
  #  if (is.numeric(labax)){ ## it means there is some default
  #    mtext(labax, at = seq(1, nrow(RP), nrow(RP)/10), side = 1, line = .5, cex = 1, font = 2)
  #    mtext(labay, at = seq(1, nrow(RP), nrow(RP)/10), side = 2, line = .5, cex = 1, font = 2)
  #  } else{
  mtext(labax, at = tstamp, side = 1, line = .5, cex = .8, font = 2, las = las)
  mtext(labay, at = tstamp, side = 2, line = .5, cex = .8, font = 2, las = las)
  
  # }
  
}

我的问题是我得到 25 个图而不是 5 个图,每个图出现 5 次,但标题不同。如果我不包括“j”部分,一切正常,但每个情节当然没有任何主标题。

感谢您的帮助。

最好, 约翰逊

【问题讨论】:

  • 什么是plotting()? 请确保您发布的代码和示例数据构成minimal reproducible example
  • 我添加了 plotting() 函数的代码。它是 crqa 包中 plotRP() 函数的调整版本
  • 您不能使用i 作为索引来引用name 中的值吗?
  • namesRP_values 有什么关系?它们的长度一样吗?
  • norie 的提示如下:pdf("filename.pdf", onefile=TRUE) for (i in 1:length(RP_values)){ parC

标签: r for-loop plot title


【解决方案1】:

根据您的描述和 cmets,您似乎需要一个元素循环而不是嵌套循环。考虑使用expand.grid 检索namesRP_values 的所有成对组合,并使用mapply 遍历它们。此外,由于parC 依赖于对应RPnrows,因此在函数内部只为两个参数定义了parC(使用更多信息名称,如title 而不是x):

plotting <- function(RP, title) {
    parC <- list(unit=100, labelx="Time", labely="Time",
                 cols="black", pcex=.01, pch=1, las=1, 
                 labax=seq(0, nrow(RP), 100), 
                 labay=seq(0, nrow(RP), 100))
    ...

    plot(tstamp, tstamp, type="n", xlab="", ylab="", 
         xaxt="n", yaxt="n", main=title)

    ...
}

params <- expand.grid(RP_values=RP_values, name=name)

out <- mapply(plotting, RP=params$RP_values, title=params$name)

【讨论】:

    猜你喜欢
    • 2019-09-03
    • 1970-01-01
    • 2020-09-02
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多