Get margin line locations (mgp) in user coordinates
Asked Answered
F

1

16

I am trying to do some non-conventional plot labeling and would like a way to convert the line parameter in mtext and axis to user coordinates.

In other words I would like to convert the values in par()$mgp to user coordinates.

This illustrates the problem:

setup_plot <- function() {
  par(mar = c(2, 10, 2, 2), oma = rep(2, 4))
  plot.new()
  plot.window(xlim = c(0, 1), ylim = c(0, 1))
  box(which = "plot", lwd = 2, col = "gray40")
  box(which = "figure", lwd = 2, col = "darkred")
  box(which = "outer", lwd = 2, col = "darkgreen")
  text(x = 0.5, y = 0.5, 
       labels = "Plot Region", 
       col = "gray40", font = 2)
  mtext(side = 3, text = "Figure region", line = 0.5, col = "darkred", font = 2)
  mtext(side = 3, text = "Device region", line = 2.5, col = "darkgreen", font = 2)
  for (i in 0:9) {
    mtext(side = 2, col = "darkred", text = paste0("Line", i), line = i)
  }
}

I have tried two different approaches.

## Try one approach where a line is the string height of "M"
setup_plot()
xline = strheight("M", units = "user")
abline(v =  par()$usr[1] - 0:9*xline, 
       xpd = TRUE, lty = "dashed", col = "gray40")

Lines do not line up with the text

## Try a second approach defining a line using par()$mai & par()$mar
setup_plot()
xline = abs(grconvertX(unique(par()$mai/par()$mar), "inches", "user"))
abline(v =  par()$usr[1] - 0:9*xline, 
       xpd = TRUE, lty = "dashed", col = "gray40")

Only one line is drawn

How do you get the line positions in user coordinates?

NOTE: The figures here are 4 inches by 6 inches. Changing the output size changes how the lines are drawn -- which also does not make sense to me.

Fleabag answered 18/3, 2015 at 14:42 Comment(2)
This isn't the answer you're looking for, but it's something you could use if all else fails: mtext(side = 2, col="gray40", text=paste(rep("_ ",45),collapse=""), line=c(0:9))Fung
That is a clever hack, but I not actually interested in drawing lines. I was just using the lines to illustrate that I do not know how to get the correct locations.Fleabag
S
15

The following should do the trick:

setup_plot()
abline(v=par('usr')[1] - (0:9) * 
         diff(grconvertX(0:1, 'inches', 'user')) * 
         par('cin')[2] * par('cex') * par('lheight'), 
       xpd=TRUE, lty=2)

margin_lines

par('cin')[2] * par('cex') * par('lheight') returns the current line height in inches, which we convert to user coordinates by multiplying by diff(grconvertX(0:1, 'inches', 'user')), the length of an inch in user coordinates (horizontally, in this case - if interested in the vertical height of a line in user coords we would use diff(grconvertY(0:1, 'inches', 'user'))).

This can be wrapped into a function for convenience as follows:

line2user <- function(line, side) {
  lh <- par('cin')[2] * par('cex') * par('lheight')
  x_off <- diff(grconvertX(0:1, 'inches', 'user'))
  y_off <- diff(grconvertY(0:1, 'inches', 'user'))
  switch(side,
         `1` = par('usr')[3] - line * y_off * lh,
         `2` = par('usr')[1] - line * x_off * lh,
         `3` = par('usr')[4] + line * y_off * lh,
         `4` = par('usr')[2] + line * x_off * lh,
         stop("side must be 1, 2, 3, or 4", call.=FALSE))
}

setup_plot()
abline(v=line2user(line=0:9, side=2), xpd=TRUE, lty=2)

EDIT: An updated version of the function, which works with logged axes, is available here.

Squinteyed answered 27/4, 2015 at 10:38 Comment(1)
I asked a follow-up question if you're interested: #30766366Fleabag

© 2022 - 2024 — McMap. All rights reserved.