Search code examples
rmathplotgraphaxis-labels

Label axis with derivative in R


What is the incatation to create math notation for an axis label of a derivative in R?

Desired output for axis label is:

enter image description here

Tried so far:

plot(0,xlab=expression(delta~y~'/'~delta~x),ylab=expression(dy/dx))

enter image description here

Documentation reviewed:


Solution

  • plot(0, xlab=expression(frac(italic(dy), italic(dx))), ylab=expression(dy/dx))
    

    You might also need to adjust positioning to get it to fit correctly. Compare:

    par(mfrow=c(1,2))
    
    plot(0, xlab=expression(frac(italic(dy), italic(dx))), ylab=expression(dy/dx))
    
    plot(0, xlab="", ylab=expression(dy/dx))
    mtext(side=1, text=expression(frac(italic(dy), italic(dx))), line=4)
    

    enter image description here

    To plot the y-label rotated, it looks like mtext doesn't support the srt (string rotation) parameter, so I guess you have to do it with text. I've placed the label manually, but you could probably do it programmatically by querying the various plot coordinate values.

    plot(0, xlab="", ylab="")
    mtext(side=1, text=expression(frac(italic(dy), italic(dx))), line=4)
    text(0.45,0, labels=expression(frac(italic(dy), italic(dx))), srt=0, xpd=TRUE)
    

    enter image description here