[英]R: creating vectors of latin/greek expression for plot titles, axis labels, or legends
I would like to merge vectors of Latin and Greek text to generate plot titles, axis labels, legend entries, etc. I have provided a trivial example below. 我想合并拉丁文和希腊文的矢量来生成情节标题,轴标签,图例条目等。我在下面提供了一个简单的例子。 I cannot figure out how to render the Greek letters in their native form.
我无法弄清楚如何以原生形式呈现希腊字母。 I have tried various combinations of
expression
, parse
, and apply
to the paste
command but I have not been able to vectorize the code that readily generates mixed Latin/Greek text for the case of a single expression (eg, expression("A ("*alpha*")")
is suitable in the case of a single expression). 我已尝试过
expression
, parse
和apply
paste
命令的各种组合,但我无法对单个表达式(例如, expression("A ("*alpha*")")
的情况下生成混合拉丁语/希腊语文本的代码进行矢量化expression("A ("*alpha*")")
适用于单个表达式)。
data<-matrix(seq(20),nrow=5,ncol=4,byrow=TRUE)
colnames(data)<-c("A","B","C","D")
greek<-c(" (alpha)"," (beta)"," (gamma)"," (delta)")
matplot(data)
legend(1,max(data),fill=c("black","red","green","blue"),apply(matrix(paste(colnames(data),greek,sep=""),nrow=4,ncol=1),1,expression))
Could you please help me with the apply()
statement within the legend()
statement? 你能帮我解决一下
legend()
语句中的apply()
legend()
语句吗? It requires some modification to produce the desired output (ie, A (α), B(β), C(γ), D(δ)). 它需要一些修改才能产生所需的输出(即A(α),B(β),C(γ),D(δ))。 Thanks in advance.
提前致谢。
Here's an alternative that avoids parse()
, and works with the example mentioned in your first comment to @mnel's nice answer: 这是一个避免使用
parse()
的替代方法,并使用你的第一个评论中提到的例子@mnel的好答案:
greek <- c("alpha", "beta", "gamma", "delta")
cnames <- paste(LETTERS[1:4], letters[1:4])
legend_expressions <-
sapply(1:4, function(i) {
as.expression(substitute(A (B),
list(A = as.name(cnames[i]), B = as.name(greek[i]))))
})
matplot(data)
legend(1,max(data),fill=c("black","red","green","blue"),legend_expressions)
Don't use apply
create a vector of expressions. 不要使用
apply
创建表达式向量。
Instead use parse(text = ...)
. 而是使用
parse(text = ...)
。
.expressions <- paste(colnames(data),greek,sep="")
legend_expressions <-parse(text = .expressions)
matplot(data)
legend(1,max(data),fill=c("black","red","green","blue"),legend_expressions)
If you want to include ~
within the expressions. 如果你想在表达式中包含
~
。 Given your current workflow, it would seem easiest to replace sep = ''
with sep = '~'
within the call to paste
鉴于您当前的工作流程,在
paste
调用中用sep = '~'
替换sep = ''
似乎最简单
.expressions <- paste(colnames(data),greek,sep="~")
legend_expressions <-parse(text = .expressions)
matplot(data)
legend(1,max(data),fill=c("black","red","green","blue"),legend_expressions)
It might even be clearer to use sprintf
to form the character strings which will become your expression vector. 甚至可以更清楚地使用
sprintf
来形成将成为你的表达向量的字符串。
If you want to include character strings that include spaces, you will need to wrap these strings in quotation marks within the string. 如果要包含包含空格的字符串,则需要将这些字符串包装在字符串中的引号中。 For example.
例如。
greek <- c("alpha", "beta", "gamma", "delta")
other_stuff <- c('hello world','again this','and again','hello')
.expressions <- mapply(sprintf, colnames(data), other_stuff, greek,
MoreArgs = list(fmt = '"%s %s"~(%s)'))
.expressions
## A B C D
## "\"A hello world\"~(alpha)" "\"B again this\"~(beta)" "\"C and again\"~(gamma)" "\"D hello\"~(delta)"
legend_expressions <-parse(text = .expressions)
matplot(data)
legend(1,max(data),fill=c("black","red","green","blue"),legend_expressions)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.