作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试做一些非常规的绘图标记,并且想要一种转换 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)
}
}
## 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")
最佳答案
以下应该可以解决问题:
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'))
)。
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)
关于r - 获取用户坐标中的边界线位置 (mgp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29125019/
我正在尝试做一些非常规的绘图标记,并且想要一种转换 line 的方法。 mtext 中的参数和 axis到用户坐标。 换句话说,我想转换 par()$mgp 中的值到用户坐标。 这说明了问题: set
?par 表示关于 mgp[1:3]: The margin line (in mex units) for the axis title, axis labels and axis line. No
?par 表示关于 mgp[1:3]: The margin line (in mex units) for the axis title, axis labels and axis line. No
我是一名优秀的程序员,十分优秀!