r - 在 r 中格式化绘图轴

标签 r formatting plot axes

我想在双对数图中绘制 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

感谢您的帮助。

斯文

最佳答案

为了精确控制坐标轴,请分别绘制它们,因此首先在 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比默认值(-0.5)小一点。棘手的一点是生成你想要的小滴答声。我只能想出这个:

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()

产生:

plot produced by axis calls

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

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()

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

关于r - 在 r 中格式化绘图轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6268871/

相关文章:

sql - 假脱机时删除前导空格和 SQL> 标签

python - 如何更改刻度之间的间距

matlab - 在 MATLAB 中将plot3 转换为 surf

R语言迭代gera斜体

R - 同一图表中不同列的ggplot多重回归线

python - 在 python 2.7.12 中格式化

r - 在 ggplot2 中使用 scale_x_date 格式化日期

r - 如何平衡不平衡分类1 :1 with SMOTE in R

在 R 中运行 Maxent

r - 在 R 中编写函数