R bloggers

grconvertX and grconvertY

FavoriteLoadingAdd to favorites

These two functions are unbelievably useful for positioning graphical elements (text, axes, labels, …) in R. They allow one to convert coordinates between various different formats. For instance, you can convert your user coordinate (say 5 where x ranges from 0 to 200) to normalized device coordinates (proportional distance across the device) and vice versa. Very useful for positioning panel labels… grconvertX(.1, “npc”, “user”) returns the x coordinate that is 10% across the plot region. So with that and the equivalent y function, you can place your labels in exactly the same position on every panel and plot… e.g. plot(rnorm(20), rnorm(20)) text(grconvertX(.1, “npc”, “user”), grconvertY(.9, “npc”, “user”), “a)”) To show the difference between the from side, run the following… text(grconvertX(.1, “npc”, “user”), grconvertY(.9, “npc”, “user”), “npc”) text(grconvertX(.1, “ndc”, “user”), grconvertY(.9, “ndc”, “user”), “ndc”, xpd = “n”) # the text will…
Original Post: grconvertX and grconvertY