R:为图标题,轴标签或图例创建拉丁/希腊表达式的矢量 [英] R: creating vectors of latin/greek expression for plot titles, axis labels, or legends

查看:89
本文介绍了R:为图标题,轴标签或图例创建拉丁/希腊表达式的矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想合并拉丁和希腊文本的向量以生成绘图标题,轴标签,图例条目等.我在下面提供了一个简单的示例.我无法弄清楚如何以其本机形式呈现希腊字母.我已经尝试对paste命令使用expressionparseapply的各种组合,但是对于单个表达式,我无法向量化容易生成混合拉丁/希腊文本的代码(例如,expression("A ("*alpha*")")适用于单个表达式).

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 (e.g., expression("A ("*alpha*")") is suitable in the case of a single expression).

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

您能否在legend()语句中的apply()语句中为我提供帮助?需要进行一些修改才能产生所需的输出(即A(α),B(β),C(γ),D(δ)).预先感谢.

Could you please help me with the apply() statement within the legend() statement? It requires some modification to produce the desired output (i.e., A (α), B(β), C(γ), D(δ)). Thanks in advance.

推荐答案

这里是一种避免使用parse()的替代方法,它可以与您对@mnel好的答案的第一个评论中提到的示例配合使用:

Here's an alternative that avoids parse(), and works with the example mentioned in your first comment to @mnel's nice answer:

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)

这篇关于R:为图标题,轴标签或图例创建拉丁/希腊表达式的矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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