Labelling panels in R graphics

March 8, 2012

(This article was first published on Insights of a PhD student » R, and kindly contributed to R-bloggers)

Labelling a graphics panel in R is easy right?

Sure it is, just use text and define the coordinates.

text(x=5, y=10, "a")

But is there an easy way to get in the same place all the time, even if you have different axis lengths (e.g. 0 to 5 on the x-axis but 0 to 100 on the y-axis)?

I don’t know if there was, but there is now!!!

Copy and run the following code into R and you have the function.

panellab <- function(perc.x, perc.y=perc.x, lab="A", pos="TR", ...){
          x <- par("usr")[2]-(((par("usr")[2]-par("usr")[1])/100)*perc.x)
          y <- par("usr")[4]-(((par("usr")[4]-par("usr")[3])/100)*perc.y)}
          x <- par("usr")[1]+(((par("usr")[2]-par("usr")[1])/100)*perc.x)
          y <- par("usr")[4]-(((par("usr")[4]-par("usr")[3])/100)*perc.y)}
          x <- par("usr")[2]-(((par("usr")[2]-par("usr")[1])/100)*perc.x)
          y <- par("usr")[3]+(((par("usr")[4]-par("usr")[3])/100)*perc.y)}
          x <- par("usr")[1]+(((par("usr")[2]-par("usr")[1])/100)*perc.x)
          y <- par("usr")[3]+(((par("usr")[4]-par("usr")[3])/100)*perc.y)}

     text(x, y, labels=lab, ...)

To use it, just specify a percentage for the x axis (and y if you want) and a position (e.g. top left, top right…).

panellab(10, lab= "a", pos="TL")

places an a 10% in from the x and y axes in the top left corner

You can add other arguments that text() takes too, such as colours.

panellab(10, lab= "a", pos="TL", col= "red")

the addition of col= “red”, makes the a red.

Simple huh!!

Hope that helps someone!!!

To leave a comment for the author, please follow the link and comment on their blog: Insights of a PhD student » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

plotly webpage

dominolab webpage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)