Use sub-/superscript and special characters in legend texts of R plots

前端 未结 2 1446
误落风尘
误落风尘 2021-02-15 11:20

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 lik

相关标签:
2条回答
  • 2021-02-15 12:03

    Change "legend_texts" to:

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

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

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

    Output:

    enter image description here

    0 讨论(0)
  • 2021-02-15 12:05

    Try this:

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

    I could not tell if you wanted Omega^10 or Omega*10 or Omega%*%10 , but they all would produce acceptable plotmath expressions.

    enter image description here

    0 讨论(0)
提交回复
热议问题