Does minor.tick of library Hmisc in R only work for primary axis (side 1 and 2) and not for secondary axis (side 3 and 4)?

StackOverflow https://stackoverflow.com/questions/17118360

  •  31-05-2022
  •  | 
  •  

Question

How to plot minor.tick in a plot for axes at sides 3 and 4 using library (Hmisc) in R? For example, the minor.tick plots minor ticks for axes (side=1 and 2) but does not produce any minor ticks for the axes (side =3 and 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)
Was it helpful?

Solution

The minor.tick function does not have the capacity to accept side arguments. It's not very big and could easily be modified so that it would accept a side argument:

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

Sometimes functions need to have their environment set to the same as the rest of the functions in the package. Not sure if this might be needed:

 environment(my.minor.tick) <- environment(minor.tick)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top