【发布时间】: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中的值吗? -
names与RP_values有什么关系?它们的长度一样吗? -
norie 的提示如下:pdf("filename.pdf", onefile=TRUE) for (i in 1:length(RP_values)){ parC