开发者

Formatting axes of plots in r

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 shoul开发者_如何学Cd be 0.5

Thanks for your help.

Sven


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:

Formatting axes of plots in r

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.


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

Formatting axes of plots in r

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜