【发布时间】:2015-03-18 14:42:13
【问题描述】:
我正在尝试做一些非常规的绘图标签,并且想要一种将mtext 和axis 中的line 参数转换为用户坐标的方法。
换句话说,我想将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)
}
}
我尝试了两种不同的方法。
## 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")
如何获取用户坐标中的线位置?
注意:这里的数字是 4 英寸 x 6 英寸。改变输出大小会改变线条的绘制方式——这对我来说也没有意义。
【问题讨论】:
-
这不是您要寻找的答案,但如果一切都失败了,您可以使用它:
mtext(side = 2, col="gray40", text=paste(rep("_ ",45),collapse=""), line=c(0:9)) -
这是一个聪明的技巧,但我实际上对画线并不感兴趣。我只是用线条来说明我不知道如何获得正确的位置。