r - 在 R 图的图例文本中使用子/上标和特殊字符

标签 r plot special-characters legend plotmath

我为多个数据集生成了一个图。每个数据集都应该有自己的图例,其中可能包含希腊字母、plotmath 符号或 sub 和 superscrition。我想循环生成图例文本。

如果只有一个图例文本,Bquote 工作正常。如果我尝试添加额外的图例文本, plotmath-commads 会丢失,...

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”更改为:

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

来自 ?legend 的帮助页面,“传奇”参数被描述为:

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



输出:

enter image description here

关于r - 在 R 图的图例文本中使用子/上标和特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15288900/

相关文章:

r - 我可以指定下载用于构建 R 包的源代码的去向吗

python - 绘制关于时间的加速度计数据

postgresql - 在grafana中显示表示时间的垂直线

vb.net - Sabre Web/.NET - SabreCommandLLSRQ 响应中的特殊字符未正确处理

ios - 卢比符号的 NSData 转换返回 nil

r - 文本层在 ggplot 中工作,但用 ggplotly 删除

r - 将社区检测与连接组件分组 igraph R 相结合

r - 用指定颜色的绘图区域外的形状进行注释

javascript - 如何将 Å、Ä 和 Ö 放入 javascript 数组中,然后将其与 html 文本进行比较?

r - 如何使图例中的标签在 R 中正确对齐?