我试图做一些非传统的情节标签和想办法的转换line参数mtext和axis用户的坐标.
换句话说,我想将值转换par()$mgp为用户坐标.
这说明了问题:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了两种不同的方法.
## 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")
Run Code Online (Sandbox Code Playgroud)
## 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")
Run Code Online (Sandbox Code Playgroud)

如何获得用户坐标中的线位置?
注意:这里的数字是4英寸×6英寸.更改输出大小会改变绘制线条的方式 - 这对我来说也没有意义.
jba*_*ums 10
以下应该做的伎俩:
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)
Run Code Online (Sandbox Code Playgroud)

par('cin')[2] * par('cex') * par('lheight')以英寸为单位返回当前行高,我们通过乘以用户坐标diff(grconvertX(0:1, 'inches', 'user'))中的英寸长度(水平地,在这种情况下 - 如果对我们将使用的用户坐标中的行的垂直高度感兴趣)转换为用户坐标diff(grconvertY(0:1, 'inches', 'user')).
为方便起见,可以将其包装到函数中,如下所示:
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)
Run Code Online (Sandbox Code Playgroud)
编辑:函数,它与记录的轴工程的更新版本,可在这里.