【问题标题】:Get margin line locations in log space获取日志空间中的边缘线位置
【发布时间】:2015-06-10 19:44:56
【问题描述】:

previous question 中,我询问了如何在用户坐标中获取边线位置。我以line2user 函数的形式收到了一个很好的答案。但是,当 x 或 y 轴处于对数刻度时,我无法弄清楚如何修改函数以使其工作。

我做了一些修改以适应对数坐标轴:

line2user <- function(line, side, log = "") {
  lh <- par('cin')[2] * par('cex') * par('lheight')
  x_off <- diff(grconvertX(0:1, 'inches', 'user'))
  y_off <- diff(grconvertY(0:1, 'inches', 'user'))
  usr <- par('usr') ## Added by me
  if (grepl("x", log)) usr[1:2] <- 10^usr[1:2] ## Added by me
  if (grepl("y", log)) usr[3:4] <- 10^usr[3:4] ## Added by me
  switch(side,
         `1` = usr[3] - line * y_off * lh,
         `2` = usr[1] - line * x_off * lh,
         `3` = usr[4] + line * y_off * lh,
         `4` = usr[2] + line * x_off * lh,
         stop("Side must be 1, 2, 3, or 4", call.=FALSE))
}

但是,我无法弄清楚如何正确调整 xoffyoff 变量以绘制正确的线条。插图:

setup_plot <- function(log = "") {
  par(mar = c(2, 10, 2, 2), oma = rep(2, 4))
  plot.new()
  plot.window(xlim = c(1, 10), ylim = c(1, 10), log = log)
  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)
  }
}

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

在考虑以下示例后,压缩线是有意义的:

plot(10)
diff(grconvertX(0:1, 'inches', 'user'))
## [1] 0.08121573 (on my device)

plot(10, log = "x")
diff(grconvertX(0:1, 'inches', 'user'))
## [1] 0.0297354 (on my device)

在使用对数刻度轴时,如何获得正确的 x_offy_off 值?

【问题讨论】:

    标签: r plot


    【解决方案1】:

    这是一个适用于对数刻度线性刻度轴的版本。诀窍是用npc 坐标而不是user 坐标来表示线位置,因为当轴在对数刻度上时后者当然不是线性的。

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

    这里有几个例子,用mar=c(5, 5, 5, 5) 应用于你的setup_plot

    setup_plot()
    axis(1, line=5)
    axis(2, line=5)
    abline(h=line2user(0:4, 1), lty=3, xpd=TRUE)
    abline(v=line2user(0:4, 2), lty=3, xpd=TRUE)
    abline(h=line2user(0:4, 3), lty=3, xpd=TRUE)
    abline(v=line2user(0:4, 4), lty=3, xpd=TRUE)
    

    setup_plot(log='x')
    axis(1, line=5)
    axis(2, line=5)
    abline(h=line2user(0:4, 1), lty=3, xpd=TRUE)
    abline(v=line2user(0:4, 2), lty=3, xpd=TRUE)
    abline(h=line2user(0:4, 3), lty=3, xpd=TRUE)
    abline(v=line2user(0:4, 4), lty=3, xpd=TRUE)
    

    setup_plot(log='y')
    axis(1, line=5)
    axis(2, line=5)
    abline(h=line2user(0:4, 1), lty=3, xpd=TRUE)
    abline(v=line2user(0:4, 2), lty=3, xpd=TRUE)
    abline(h=line2user(0:4, 3), lty=3, xpd=TRUE)
    abline(v=line2user(0:4, 4), lty=3, xpd=TRUE)
    

    setup_plot(log='xy')
    axis(1, line=5)
    axis(2, line=5)
    abline(h=line2user(0:4, 1), lty=3, xpd=TRUE)
    abline(v=line2user(0:4, 2), lty=3, xpd=TRUE)
    abline(h=line2user(0:4, 3), lty=3, xpd=TRUE)
    abline(v=line2user(0:4, 4), lty=3, xpd=TRUE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      相关资源
      最近更新 更多