Formatting graph axes in r

I want to build 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")

xtics installed in

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

How can I determine:

  • so that the labels are indicated for the main decimal places (1,0, 0,1, 0,01, 0,001, 0,0001, ...)

  • so that the figures are displayed for 9 positions between decimal positions (for the area from 0.01 to 0.1, this would be 0.01, 0.02, 0.03, ....)

  • that the maximum y-range should be 0.5

Thank you for your help.

Sven

+3
source share
2 answers

To precisely control the axes, draw them separately, so first suppress the axes using the argument axes = FALSEin the call plot():

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

2 2, , , tcl axis() , ( -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, ? y ylim plot(). (min max)

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

enter image description here

+1

All Articles