Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Rotate text independent of scaling (using mtext)?

Tags:

plot

r

graphics

There are two ways to create rotated axis labels in R:

  • text(srt = angle)

    • Advantage: Arbitrary angle possible (like 45°)
    • Disadvantage: y position changes with y scaling
  • mtext(las = 1)

    • Advantage: y position fixed independent of y scaling
    • Disadvantages: Only discrete values possible (90° steps)

Is there a way to add rotated axis labels of whom the y position does not depend on the scaling of the y axis?

like image 349
NicolasBourbaki Avatar asked Sep 03 '25 06:09

NicolasBourbaki


1 Answers

Thinking about the problem, I found a solution making use of the ylim parameter to the plot() function:

ylim = c(0, 0.5)
plot(...)
text(..., srt = 45, y = 0.2 * (ylim[2] - ylim[1]))

dynamically adjusts the position of the text according to the scaling of the y axis.

like image 83
NicolasBourbaki Avatar answered Sep 04 '25 21:09

NicolasBourbaki