如何使用原子向量作为R中图形标题的字符串 [英] how to use an atomic vector as a string for a graph title in R

查看:577
本文介绍了如何使用原子向量作为R中图形标题的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从R中的z分数矩阵绘制一个图,我想要使用列标题作为标题的一部分构建一个遍历每列的函数,并将每个图保存为png。我想我知道如何做迭代并将图形保存为PNG,但我陷入了使用向量作为字符串的困境。我尝试上传没有列标题的矩阵,然后将矩阵[1,]存储为变量'标题'以供使用。然后我试图绘制:

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


我得到:

 警告信息:
在Ops.factor(左,右):+对于因素
没有意义

'+'表示:

 错误:意外符号在... 

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



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

<$ (1:30,rnorm(30),ylim = c(-10,10),yaxs =i,xlab =Region,ylab =Z-Score ,main =$ header [i],type =n)的Z分数)

我得到:

pre $ 在$ header中的Z分数错误
$运算符对原子向量无效

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

解决方案

如果要将变量值插入到标题的字符串中, bquote 是要走的路:

 标题<  -  c (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 的帮助页面获取更多信息。


I'm trying to plot a graph from a matrix of z-scores in R, i would like to build a function to iterate through each column using the column header as part of the title and saving each graph as a png.I think I know how to do the iteration and saving graphs as pngs but I am getting stuck with using the vector as a string. I tried to upload the matrix with no column headers and then store matrix[1,] as a variable 'headers' to use. Then I tried to plot:

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

I get:

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

I try without the '+' and it says:

 Error: unexpected symbol in ...

So then I looked around and found 'paste(headers[i],collapse=" ") which I though I could substitute in but it just puts the number '28' as the title.

I've tried what I thought was another potential solution:

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

and I get:

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

I'm new to R and this seems like something that would be so simple if I happened to stumble across the right guide/tutorial after hours of google searching but I really don't have that kind of time on my hands. Any suggestions, pointers or solutions would be great??

解决方案

If you want to insert values from variables into strings for a plot title, bquote is the way to go:

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

Have a look at the help page of ?bquote for further information.

这篇关于如何使用原子向量作为R中图形标题的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆