Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mtext() to add horizontal y labels

Tags:

plot

r

I know it is going to be quick and simple for some of you. I just want to have a horizontal y axis label using mtext(). It has to do with adj I guess, but I have been wasting the last 2 hours trying to figure it out... In the following examples, I just want the y-labels (myLab) to be horizontal and left justified.

myLab <- c("aaaaaaa", "bb", "c")

par(oma=c(0,10,0,0)) # make a large left maring for the labels

plot(x=c(1:3),
     y=c(1:3),
     pch="|",
     lwd=3,
     tck=0.01,
     yaxt="n",
     ylab="",
     xlab="my legend",
     at= c(1:3),
     )


mtext(text=myLab,
      side=2,
      outer = FALSE,
      at=c(1:3)
      )

I do not need to use mtext(). If you have a better alternative, please let me know.

Thanks!

like image 604
Olivier Avatar asked Aug 30 '11 20:08

Olivier


1 Answers

Use the las argument:

mtext(text=myLab, las=1,
       side=2,
       outer = FALSE,
       at=c(1:3)
       )

Brings them right up the edge but you can pad with trailing spaces in the myLab values. You can left justify using an adj value of 0 (versus the default value of 1):

 plot(x=c(1:3),
      y=c(1:3),
      pch="|",
      lwd=3,
      tck=0.01,
      yaxt="n",
      ylab="",
      xlab="my legend"  # removing extraneous `at` value that only throws a warning
      )
 mtext(text=myLab, las=1, adj=0,
        side=2,
        outer = FALSE,
        line=3.5, at=1:3
        )
like image 83
IRTFM Avatar answered Sep 26 '22 21:09

IRTFM