设置 r 中绘图轴的格式

发布于 2024-11-14 10:58:39 字数 489 浏览 3 评论 0原文

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

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

感谢您的帮助。

斯文

I want to plot a beta distribution in a double logarithmic plot.

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

The xtics are set at

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

How can I determine:

  1. that labels are given for the main decimal positions (1.0, 0.1, 0.01, 0.001, 0.0001,...)

  2. that tics should be drawn for 9 position between the decimal positions (for the region between 0.01 and 0.1 it would be 0.01, 0.02, 0.03,....)

  3. that the maximum y-range should be 0.5

Thanks for your help.

Sven

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

写给空气的情书 2024-11-21 10:58:39

为了更好地控制轴,请分别绘制它们,因此首先在 plot() 调用中使用参数 axes = FALSE 抑制轴:

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

然后根据需要添加轴 问题 2 可以用同样的方式回答

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

,您只需要指定刻度线的位置,也许将 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()

生成:

由轴调用生成的图

至于问题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()

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

For fine control of the axes, plot them separately, so first suppress the axes by using argument axes = FALSE in the plot() call:

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

Then add the axes as you want them

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

Question 2 can be answered in the same way, you just need to specify the locations for the tick marks, perhaps setting argument argument tcl in the axis() call to be a bit smaller than default (which is -0.5). The tricky bit is in generating the minor ticks you want. I could only come up with this:

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

or

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

which both give:

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

We use them via another call to axis():

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

We suppress labels here using labels = NA. You just need to work out how to do the vectors for at...

Putting the two steps together we have:

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

Which produces:

plot produced by axis calls

As for question 3, what do you mean the maximum range? You can set the limits on the y-axis using the ylim argument to plot(). You provide the limits (min and max) like so

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

But a range on it's own is not sufficient to define the limits, you'd need to tell us one of the min or max values to show on the plot or the actual range of values you want.

百思不得你姐 2024-11-21 10:58:39

试试这个:

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

在此处输入图像描述

Try this:

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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文