简体   繁体   中英

Plot legend with subscript and insert stored value in R

Trying to add a legend to my plot, which should display some fitting parameters I've calculated earlier, subscripts AND a new line for every parameter, but I can't seem to make a legend that works with eg substitute() and paste() together. legend() always seems to break with a argument "legend" is missing, with no default . Apparently substitute() can't do new lines, and mtext() is a bit... bothersome, compared to the human-readable "bottomright" parameter in legend() .

This is as far as I've gotten:

paramX = 1234
paramY = 9876

plot.new()

legend("bottomright",
       bty = "n",
       paste("x[max] = ", paramX,
             "\ny[max] = ", paramY)
)
paramX = 1234
paramY = 9876

plot.new()

expr <- vector("expression", 2)
expr[[1]] <- bquote(x[max]==.(paramX))
expr[[2]] <- bquote(y[max]==.(paramY))

legend("bottomright",
       bty = "n",
       legend = expr)

Plots this result:

结果图

Relevant documentation: help("legend"); help("plotmath"); help("bquote") help("legend"); help("plotmath"); help("bquote") help("legend"); help("plotmath"); help("bquote") .

The most difficult part is combining two bquote expressions into one expression vector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM