0

我正在尝试从 R 中的 z 分数矩阵绘制图表,我想构建一个函数来使用列标题作为标题的一部分遍历每一列并将每个图表保存为 png。我想我知道如何进行迭代并将图形保存为 png,但我一直坚持使用向量作为字符串。我尝试上传没有列标题的矩阵,然后将 matrix[1,] 存储为要使用的变量“标题”。然后我尝试绘制:

 plot(1:30, rnorm(30), ylim=c(-10,10), yaxs="i", xlab = "Region", ylab = "Z-Score",main = "CNV plot of " + headers[i], type = "n")

我得到:

 Warning message:
 In Ops.factor(left, right) : + not meaningful for factors

我尝试不使用“+”,它说:

 Error: unexpected symbol in ...

所以然后我环顾四周,找到了 'paste(headers[i],collapse="") ,虽然我可以替换它,但它只是将数字 '28' 作为标题。

我已经尝试了我认为是另一个潜在的解决方案:

 plot(1:30, rnorm(30), ylim=c(-10,10), yaxs="i", xlab = "Region", ylab = "Z-Score",main = "Z-scores of " $headers[i], type = "n")

我得到:

 Error in "Z-scores of "$headers : 
 $ operator is invalid for atomic vectors

我是 R 新手,如果我在谷歌搜索数小时后碰巧偶然发现了正确的指南/教程,这似乎会变得如此简单,但我真的没有那种时间在我手上。任何建议,指针或解决方案都会很棒?

4

2 回答 2

2

paste("CNV plot of", headers[i]), 应该管用。collapse仅当您粘贴长度大于一的向量时才需要(headers[i]即使header不是,也应该是一个长度)。R 没有任何连接运算符,不像 PHP、JS 等(所以+, &,.不起作用,你必须使用paste)。

请注意,您的 paste 是paste(headers[i],collapse=" "),如果只是 ploted 28,则表明您的headers矢量不包含您认为的内容(如果您不想28显示,那就是。

尝试循环浏览您的矢量并将粘贴命令打印到屏幕上以查看它显示的内容(并且,只需打印矢量)。

于 2014-01-24T12:59:25.480 回答
2

如果要将变量中的值插入到绘图标题的字符串中,bquote可以采用以下方法:

headers <- c(28, 14, 7) # an examle
i <- 1

plot(1:30, rnorm(30), ylim=c(-10,10), yaxs="i",
     xlab = "Region", ylab = "Z-Score", type = "n",
     main = bquote("CNV plot of" ~ .(headers[i])) )

在此处输入图像描述

查看帮助页面?bquote了解更多信息。

于 2014-01-24T13:08:36.903 回答