【问题标题】:Formatting axes of plots in r在 r 中格式化绘图轴
【发布时间】:2011-06-07 16:58:20
【问题描述】:

我想在双对数图中绘制 beta 分布。

x <- seq(0, 1, length=1001)
y <- dbeta(x, 0.1, 0.1)
plot(x, y, type="h", log="xy")

xtics 设置在

0.001
0.005
0.01 (without label)
0.05
0.1 (without label)
0.5
1 (without label)

如何判断:

  1. 为主要小数位(1.0、0.1、0.01、0.001、0.0001、...)给出标签

  2. 应该在小数位之间的 9 位上绘制抽动(对于 0.01 和 0.1 之间的区域,它将是 0.01、0.02、0.03、....)

  3. 最大 y 范围应为 0.5

感谢您的帮助。

斯文

【问题讨论】:

    标签: r formatting plot axes


    【解决方案1】:

    为了对轴进行精细控制,请分别绘制它们,因此首先在 plot() 调用中使用参数 axes = FALSE 来抑制轴:

    plot(x, y, type="h", log="xy", axes = FALSE)
    

    然后根据需要添加轴

    axis(side = 1, at = (locs <- 1/c(1,10,100,1000)), labels = locs)
    axis(side = 2)
    box()
    

    问题 2 可以用同样的方式回答,您只需要指定刻度线的位置,也许将 axis() 调用中的参数参数 tcl 设置为比默认值小一点(即 @987654328 @)。棘手的一点是生成您想要的次要刻度。我只能想出这个:

    foo <- function(i, x, by) seq(x[i,1], x[i, 2], by = by[i])
    locs2 <- unlist(lapply(seq_along(locs[-1]), FUN = foo, 
                           x= embed(locs, 2), by = abs(diff(locs)) / 9))
    

    locs2 <- c(outer(1:10, c(10, 100, 1000), "/"))
    

    两者都给出:

    R> locs2
     [1] 0.100 0.200 0.300 0.400 0.500 0.600 0.700 0.800 0.900 1.000 0.010 0.020
    [13] 0.030 0.040 0.050 0.060 0.070 0.080 0.090 0.100 0.001 0.002 0.003 0.004
    [25] 0.005 0.006 0.007 0.008 0.009 0.010
    

    我们通过对axis() 的另一个调用来使用它们:

    axis(side = 1, at = locs2, labels = NA, tcl = -0.2)
    

    我们在这里使用labels = NA 隐藏标签。你只需要弄清楚如何为at做向量...

    将这两个步骤放在一起:

    plot(x, y, type="h", log="xy", axes = FALSE)
    axis(side = 1, at = (locs <- 1/c(1,10,100,1000)), labels = locs)
    axis(side = 1, at = locs2, labels = NA, tcl = -0.3)
    axis(side = 2)
    box()
    

    产生:

    至于问题3,最大范围是什么意思?您可以使用ylim 参数设置y 轴上的限制plot()。您像这样提供限制(最小值和最大值)

    plot(x, y, type="h", log="xy", axes = FALSE, ylim = c(0.2, 1))
    axis(side = 1, at = (locs <- 1/c(1,10,100,1000)), labels = locs)
    axis(side = 2)
    box()
    

    但仅凭一个范围不足以定义限制,您需要告诉我们要在绘图上显示的最小值或最大值之一或您想要的实际值范围。

    【讨论】:

      【解决方案2】:

      试试这个:

      library(sfsmisc)
      
      x <- seq(0, 1, length=1001)
      y <- dbeta(x, 0.1, 0.1)
      plot(x, y, type="h", log="xy", xaxt="n", yaxt="n", ylim=c(0.01, 0.5), main="Title")
      
      atx <- c(0.0001, 0.001, 0.01, 0.1, 1, 10, 100)
      eaxis(1, at=atx, labels=format(atx, drop0trailing=TRUE, scientific=FALSE), drop.1=FALSE, small.mult=10 )
      aty <- c(0.01, 0.1, 0.5, 1, 10, 100)
      eaxis(2, at=aty, labels=format(aty, drop0trailing=TRUE, scientific=FALSE), drop.1=FALSE, small.mult=10 )
      grid()
      

      【讨论】:

        猜你喜欢
        • 2018-05-19
        • 1970-01-01
        • 2016-10-14
        • 2019-10-15
        • 1970-01-01
        • 2021-04-03
        • 2019-09-21
        • 2016-12-14
        • 2012-09-04
        相关资源
        最近更新 更多