1

如何使用 R 中的库(Hmisc)在第 3 侧和第 4 侧的轴的图中绘制 minor.tick?例如,minor.tick 绘制轴(side=1 和 2)的次要刻度,但不会为轴(side =3 和 4)生成任何次要刻度

rain1=c(0,100,200,300)
plot(data$TimeVariable2A,data$Variable2A,axes=FALSE,xlab="",ylab="",xlim=c(0,16),
     ylim=c(0,40),xaxs="i",yaxs="i",pch=19)
lines(data$TimeVariable3A,data$Variable3A)
axis(2,tick=T,at=seq(0,40,by=10),label= seq(0,40,by=10))
axis(1,tick=T,at=seq(0,16,by=4),label=seq(0,16,by=4))

par(new=TRUE)
plot(data$TimeVariable1A,data$Variable1A,axes=FALSE,xlab="",ylab="",type="l",ylim=c(800,0),xaxs="i",yaxs="i")
axis(4,tick=TRUE,at=rain1,label=rain1,col.axis="blue",col="blue")
axis(3,xlim=c(0,16),tick=T,at=seq(0,16,by=4),label=seq(0,16,by=4),col.axis="blue",col="blue")
polygon(data$TimeVariable1A,data$Variable1A,col='blue',border=NA)
minor.tick(nx=5,ny=5)
4

1 回答 1

4

minor.tick函数没有接受边参数的能力。它不是很大,可以很容易地修改,以便它接受一个侧面参数:

my.minor.tick <-
function (nx = 2, ny = 2, tick.ratio = 0.5, side=NA)  
{ # added the side parameter
    ax <- function(w, n, tick.ratio) {
        range <- par("usr")[if (w == "x") 
            1:2
        else 3:4]
        tick.pos <- if (w == "x") 
            par("xaxp")
        else par("yaxp")
        distance.between.minor <- (tick.pos[2] - tick.pos[1])/tick.pos[3]/n
        possible.minors <- tick.pos[1] - (0:100) * distance.between.minor
        low.minor <- min(possible.minors[possible.minors >= range[1]])
        if (is.na(low.minor)) 
            low.minor <- tick.pos[1]
        possible.minors <- tick.pos[2] + (0:100) * distance.between.minor
        hi.minor <- max(possible.minors[possible.minors <= range[2]])
        if (is.na(hi.minor)) 
            hi.minor <- tick.pos[2]
        if (.R.)        # Next three lines have only the modifications
            axis(if (w == "x" & is.na(side) )
                1
            else if (!is.na(side)) side
            else 2, seq(low.minor, hi.minor, by = distance.between.minor), 
                labels = FALSE, tcl = par("tcl") * tick.ratio)
        else axis(if (w == "x") 
            1
        else 2, seq(low.minor, hi.minor, by = distance.between.minor), 
            labels = FALSE, tck = par("tck") * tick.ratio)
    }
    if (nx > 1) 
        ax("x", nx, tick.ratio = tick.ratio)
    if (ny > 1) 
        ax("y", ny, tick.ratio = tick.ratio)
    invisible()
}

有时函数需要将其环境设置为与包中的其余函数相同。不确定这是否需要:

 environment(my.minor.tick) <- environment(minor.tick)
于 2013-06-14T23:37:14.200 回答