代码之家  ›  专栏  ›  技术社区  ›  R_User

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

  •  8
  • R_User  · 技术社区  · 11 年前

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

    如果只有一个图例文本,Bquote可以正常工作。如果我试图添加额外的图例文本,绘图命令就会丢失,。。。

    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
      )
    
    2 回复  |  直到 11 年前
        1
  •  6
  •   IRTFM    11 年前

    试试这个:

     legend_texts = expression(
       Omega^2, Omega*10)
    
     legend(
       "topleft"
       , legend = legend_texts
       , col = c(1:2)
       , pch = c(1:2)
       , lty = 1
       )
    

    我不知道你是否愿意 Omega^10 Omega*10 Omega%*%10 ,但它们都会生成可接受的plotmath表达式。

    enter image description here

        2
  •  4
  •   A5C1D2H2I1M1N2O1R2T1    11 年前

    将“图例_文本”更改为:

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

    从的帮助页面 ?legend ,“图例”参数描述为:

    字符或表达载体。长度为1,将显示在图例中。其他对象将被as.graphicsAnnot强制。

    输出:

    enter image description here