在R图的图例文本中使用子/上标和特殊字符 [英] Use sub-/superscript and special characters in legend texts of R plots

查看:380
本文介绍了在R图的图例文本中使用子/上标和特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为多个数据集生成一个图.每个数据集都应拥有自己的图例,其中可能包含希腊字母,绘图符号或子和超写.我想循环生成图例文本.

I generate a plot for multiple datasets. Each of the datasets should get it's own legend, which might contain greek letters, plotmath symbols or sub and superscrition. I'd like to generate the legend-texts in a loop.

如果只有一个图例文本,则Bquote可以正常工作.如果我尝试添加附加的图例文字,则绘图迷宫迷路者会迷路,...

Bquote works fine, if there is only one legend text. If I try to add additinal legend texts, the plotmath-commads get lost,...

x <- 0:10
y1 = x * x
y2 = x * 10

plot (1,1, type="n", xlab=bquote(Omega), ylab="Y", las=1, xlim=range(x), ylim=range(y1, y2))
lines(x, y1, col=1, pch=1, type="b")
lines(x, y2, col=2, pch=2, type="b")

# generate legend texts (in a loop)
legend_texts = c(
  bquote(Omega^2)
  , bquote(Omega%*%10)
)
# Using a single bquote works fine:
#legend_texts = bquote(Omega^2)
#legend_texts = bquote(Omega%*%10)

legend(
  "topleft"
  , legend = legend_texts
  , col = c(1:2)
  , pch = c(1:2)
  , lty = 1
  )

推荐答案

将"legend_texts"更改为:

Change "legend_texts" to:

# generate legend texts (in a loop)
legend_texts = c(
  as.expression(bquote(Omega^2))
  , as.expression(bquote(Omega%*%10))
)

?legend的帮助页面中,传奇"参数描述为:

From the help page for ?legend, the "legend" argument is described as:

字符或表达载体.长度≥1的字符出现在图例中.其他对象将由as.graphicsAnnot强制.

a character or expression vector. of length ≥ 1 to appear in the legend. Other objects will be coerced by as.graphicsAnnot.

输出:

这篇关于在R图的图例文本中使用子/上标和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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