使用lapply标记一系列带字母的图

时间:2013-08-02 20:43:36

标签: r

我有一个小函数可以创建一个累积分布图,我使用lapply在数据集上运行(数据集是按分析物细分的列表)。我想函数将图“a)”标记为“g)”,但似乎无法弄清楚如何获得lapply来生成顺序字母。有任何想法吗?代码如下。

cdf.int <- function(data.set) {
  plot(ecdf(log10(data.set$CONC_CALC)), main = "", xlim = c(-3.0, 3.0))
  text(-4, 1.2, "a)", xpd = TRUE)
}

pdf(paste("output/figures/CDF_RegionalData.pdf", sep = ""), 
  height = 11, width = 8)
par(las = 1, omi = c(0.5, 1.0, 1.5, 1.0), mfrow = c(5, 2), ps = 10, family = "sans", mar = c(3.0, 3.0, 3.0, 3.0))
lapply(split.rd, cdf.int) 
dev.off() 

2 个答案:

答案 0 :(得分:0)

这是使用lapply/sapply的缺点的一个示例...列表元素的名称不会传递给函数,只有值才会传递给函数。因此,在使用lapply(或者names(yourlist))并使用以下内容从“您的列表”中提取时,您可以解决这个问题:

1:length(yourlist)

在你的情况下,这意味着你需要重写你的功能。

答案 1 :(得分:0)

这就像一个梦想

cdf.int <- function(n) {
  plot(rnorm(10), main = paste0(letters[n], ")"), xlim = c(-3.0, 3.0))
  text(-4, 1.2, "a)", xpd = TRUE)
}

pdf("test.pdf", height = 11, width = 8)
par(las = 1, omi = c(0.5, 1.0, 1.5, 1.0), mfrow = c(5, 2), ps = 10, family = "sans", mar = c(3.0, 3.0, 3.0, 3.0))

lapply(1:6, cdf.int) 
dev.off()

使用mapply

的一个(只是一点点)更现实的例子
ll <- list(a=rnorm(10),
           b=rnorm(10))

cdf.int <- function(data, name) {
  plot(data, main = name, xlim = c(-3.0, 3.0))
  text(-4, 1.2, "a)", xpd = TRUE)
}

pdf("test.pdf", height = 11, width = 8)
par(las = 1, omi = c(0.5, 1.0, 1.5, 1.0), mfrow = c(2, 1), ps = 10, family = "sans", mar = c(3.0, 3.0, 3.0, 3.0))

mapply(cdf.int, ll, names(ll)) 
dev.off() 
相关问题