Search code examples
rplotr-base-graphics

r plot xlab for side 3 and side 4


In R is there a way to have titles for side 3 (top) = x2 and side 4 (right) = y2 like highlighted below?

Below is R code.

x<-1:4; y=x*x
  
plot(x, y, pch=18, col="red", type="b",
     frame=FALSE, xaxt="n") # Remove x axis
axis(1, 1:4, LETTERS[1:4], col.axis="blue")
axis(3, col = "darkgreen", lty = 2, lwd = 0.5)
axis(4, col = "violet", col.axis = "dark violet", lwd = 2)

enter image description here


Solution

  • You can add labels to additional axes using mtext(). For the right y axis you will probably need to add extra space using par(mar = ...)

    par(mar = c(5, 4, 4, 5)) # create extra space for label
    plot(1:5)
    axis(3)
    axis(4)
    mtext("top xaxis", side = 3, line = 2)
    mtext("right y axis needs extra space", side = 4, line = 2)
    

    Created on 2022-11-08 with reprex v2.0.2