rlegendsuperscriptitalics

How to superscript and italicize in the same plot in R


I'm trying to create the following legend:

legend(x = 23, y = 40, legend = c('Col-0 Control', 'Col-0 840g ha-1', 'TOC1-ox Control', "TOC1 840g ha-1"), col = c('black', 'black','red', 'red'), lty = c(1,2,1,2 ), pch=c(15,15,15,15), pt.bg = c('white', 'white', NA,NA), cex = 1.5, bty="n", lwd=2)

Which works but I am trying to get the ha-1 in the superscript format ha^-1 while also making just the TOC1 (not -ox or 840g ha-1) in italics. I've tried the following:

as.expression(bquote("Col-0 840g " ~ ha^-1 ~)

which worked on its own but does not seem to work when i combined the other 3 labels. I'm also lost on how to italicize just TOC1 and have tried the method outlined here: How to only make one legend name italic in base R plot? with no luck. Any help would be very very appreciated!!

code so far:

windows(width=10, height=8)
windowsFonts(A = windowsFont("Times New Roman"))
plot.new()
op <- par(family="A", font=2)
plot(Exp24[1:3,12], Exp24[1:3,11], bty = "n", xaxt = "n", xlim=range(24:72), ylim=range(10:40), col = "black", type = "b", pch=15, cex=2, family = "A", xlab = "Time After Leaf Disc Extraction (h)", ylab = "Relative Electrolyte Leakage (%)", lty = 1, lwd=2, cex.lab =1.5, cex.axis=1.5, font=2, font.lab=2)
arrows(Exp24[1:3,12], Exp24[1:3,11]-Exp24[1:3,15], Exp24[1:3,12], Exp24[1:3,11]+Exp24[1:3,15], length=0.05, angle=90, code=3, col = "black", lwd=2)
axis(side=1,at=c(24,48, 72),labels=c("24","48","72"), font=2, cex.axis=1.5, family="A", lwd=2)
par(op)
legend(x = 23, y = 40, legend = c(expression('Col-0 Control'), expression('Col-0 840g ha'^'-1'), expression(italic('TOC1')*'-ox Control'), expression(italic('TOC1')*'-ox 840g ha'^'-1')), col = c('black', 'black','red', 'red'), lty = c(1,2,1,2 ), pch=c(15,15,15,15), pt.bg = c('white', 'white', NA,NA), cex = 1.5, bty="n", lwd=2)
par(op)

Solution

  • You can combine superscript and italic text in a legend like this:

    # make a very simple plot
    x <- c(1,3,6,9,12)
    y <- c(1.5,2,7,8,15)
    plot(x,y, xlab=expression('AB'^'superscript'* italic('some italic text'))))
    

    The asterix * denotes the end of the superscript formatting. Text that should be italic is placed in italic(...)

    Output:

    enter image description here

    Now if you have several labels in your legend, just put every label in one expression(...) function. Like this

    c(expression('cars'), expression('trucks'^'trucks'* italic(trucks))