获取用户坐标中的边距线位置(mgp) [英] Get margin line locations (mgp) in user coordinates

查看:107
本文介绍了获取用户坐标中的边距线位置(mgp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些非常规的绘图标签,并且希望将mtextaxis中的line参数转换为用户坐标.

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.

换句话说,我想将par()$mgp中的值转换为用户坐标.

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

这说明了问题:

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")

## 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")

如何获取用户坐标中的线位置?

How do you get the line positions in user coordinates?

注意::这里的数字是4英寸乘6英寸.更改输出大小会更改线条的绘制方式-这对我来说也没有意义.

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.

推荐答案

以下方法可以解决问题:

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)

par('cin')[2] * par('cex') * par('lheight')返回以英寸为单位的当前行高,我们将乘以用户坐标中英寸的长度diff(grconvertX(0:1, 'inches', 'user'))来转换为用户坐标(水平,在这种情况下,如果对用户坐标中的行,我们将使用diff(grconvertY(0:1, 'inches', 'user'))).

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)


可在此处获得该功能的更新版本,该版本适用于记录的轴. /strong>


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

这篇关于获取用户坐标中的边距线位置(mgp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆