Handling axis with dates in twoord.plot (remove axis 3)

六月ゝ 毕业季﹏ 提交于 2019-12-10 11:06:03

问题


I am trying to use twoord.plot with different dates on x axis and counts for ly and percentages for ry, and I want to remove axis 3 or change its color. Setting axes = F does not work, neither does xaxt = 'n'.

In fact, axes = FALSE gives me this error message:

Error in plot.default(lx, ly, xlim = xlim, ylim = lylim, xlab = xlab, : formal argument "axes" matched by multiple actual arguments

The solution given here:

Remove all axis values and labels in twoord plot does not work for me with different rx and lx.

Here is a similar code to that I'm using:

set.seed(123)
library(plotrix)
twoord.plot(
  lx = seq(as.Date("2016-01-01"), as.Date("2017-01-01"), by = "days"),
  ly = round(runif(367) * 6),
  rx = seq(as.Date("2016-05-09"), as.Date("2017-01-12"), by = "days"),
  ry = sort(rnorm(249, 60, 10)),
  lylim = range(round(runif(367) * 6)) + c(0, 10), 
  rylim = range(sort(rnorm(249, 60, 10))) + c(-35, 10), 
  ylab.at = mean(round(runif(367) * 6)),
  rylab.at = mean(sort(rnorm(249, 60, 10))),
  rylab = "Percentages %", ylab = "No. of x",
  type = c("l", "l"), lcol = "skyblue4", rcol = "chocolate1" ,
  xtickpos = as.numeric(seq(as.Date("2016-01-01"), as.Date("2017-01-01"), by = "months")),
  xticklab = seq(as.Date("2016-01-01"), as.Date("2017-01-01"), by = "months"))

Any ideas on how to this? Thanks in advance! :)


回答1:


Just hack the twoord.plot function. Here's an example to make all axis labels black (Use twoord.plot2 instead of twoord.plot when creating the plot). If you want to remove the axes altogether, just comment (add # before the lines) Lines 88-90 (For ly) and Lines 123-125 (for ry). I have left notes for them in the function below.

twoord.plot2 = function (lx, ly, rx, ry, data = NULL, main = "", xlim = NULL, 
    lylim = NULL, rylim = NULL, mar = c(5, 4, 4, 4), lcol = 1, 
    rcol = 2, xlab = "", lytickpos = NA, ylab = "", ylab.at = NA, 
    rytickpos = NA, rylab = "", rylab.at = NA, lpch = 1, rpch = 2, 
    type = "b", xtickpos = NULL, xticklab = NULL, halfwidth = 0.4, 
    axislab.cex = 1, do.first = NULL, ...) 
{
    if (!is.null(data)) {
        ly <- unlist(data[ly])
        ry <- unlist(data[ry])
        if (missing(lx)) 
            lx <- 1:length(ly)
        else lx <- unlist(data[lx])
        if (missing(rx)) 
            rx <- 1:length(ry)
        else rx <- unlist(data[rx])
    }
    if (missing(lx)) 
        lx <- 1:length(ly)
    if (missing(ry)) {
        if (missing(rx)) {
            rx <- 1:length(ry)
            ry <- ly
            ly <- lx
            lx <- 1:length(ly)
        }
        else {
            ry <- rx
            rx <- 1:length(ry)
        }
    }
    oldmar <- par("mar")
    par(mar = mar)
    if (is.null(xlim)) 
        xlim <- range(c(lx, rx))
    if (missing(lx)) 
        lx <- 1:length(ly)
    if (is.null(lylim)) {
        lylim <- range(ly, na.rm = TRUE)
        lyspan <- diff(lylim)
        if (lyspan == 0) 
            lyspan <- lylim[1]
        lylim[2] <- lylim[2] + lyspan * 0.04
        if (lylim[1] != 0) 
            lylim[1] <- lylim[1] - lyspan * 0.04
    }
    if (length(type) < 2) 
        type <- rep(type, 2)
    if (match(type[1], "bar", 0)) {
        oldcex <- par(cex = axislab.cex)
        plot(lx, ly, xlim = xlim, ylim = lylim, xlab = xlab, 
            ylab = "", yaxs = "i", type = "n", main = "", axes = FALSE, 
            ...)
        par(oldcex)
        if (!is.null(do.first)) 
            eval(parse(text = do.first))
        ybottom <- par("usr")[3]
        if (lylim[1] < 0) 
            abline(h = 0, lty = 2)
        rect(lx - halfwidth, ifelse(ly < 0, ly, ybottom), lx + 
            halfwidth, ifelse(ly > 0, ly, 0), col = lcol)
    }
    else {
        oldcex <- par(cex = axislab.cex)
        plot(lx, ly, xlim = xlim, ylim = lylim, xlab = xlab, 
            ylab = "", yaxs = "i", type = "n", main = "", axes = FALSE, 
            ...)
        par(oldcex)
        if (!is.null(do.first)) 
            eval(parse(text = do.first))
        points(lx, ly, col = lcol, pch = lpch, type = type[1], 
            ...)
    }
    title(main = main)
    xylim <- par("usr")
    box()
    if (is.null(xticklab)) 
        axis(1, cex.axis = axislab.cex)
    else {
        if (is.null(xtickpos)) 
            xtickpos <- 1:length(xticklab)
        axis(1, at = xtickpos, labels = xticklab, cex.axis = axislab.cex)
    }
    if (is.na(lytickpos[1])) 
        lytickpos <- pretty(ly)
    if (is.na(ylab.at)) 
        ylab.at <- mean(lytickpos)
    color.axis(2, at = lytickpos, axlab = ylab, axlab.at = ylab.at, #LINE 88
        col = "black", cex.axis = axislab.cex, #col = ifelse(is.na(lcol), 1, lcol), cex.axis = axislab.cex, 
        cex = axislab.cex) #LINE 90
    if (is.null(rylim)) {
        rylim <- range(ry, na.rm = TRUE)
        ryspan <- diff(rylim)
        if (ryspan == 0) 
            ryspan <- rylim[1]
        rylim[2] <- rylim[2] + ryspan * 0.04
        if (rylim[1] != 0) 
            rylim[1] <- rylim[1] - ryspan * 0.04
    }
    ymult <- diff(lylim)/diff(rylim)
    yoff <- lylim[1] - rylim[1] * ymult
    if (match(type[2], "bar", 0)) {
        if (rylim[1] < 0) 
            abline("h", 0)
        rect(rx - halfwidth, ifelse(ry < 0, ry, rylim[1] * ymult + 
            yoff), rx + halfwidth, ifelse(ry > 0, ry * ymult + 
            yoff, 0), col = rcol)
    }
    else points(rx, ry * ymult + yoff, col = rcol, pch = rpch, 
        type = type[2], ...)
    if (is.na(rytickpos[1])) 
        rylabels <- pretty(rylim)
    else rylabels <- rytickpos
    if (min(rylabels) < rylim[1]) 
        rylabels <- rylabels[rylabels >= rylim[1]]
    if (max(rylabels) > rylim[2]) 
        rylabels <- rylabels[rylabels <= rylim[2]]
    axat <- rylabels * ymult + yoff
    if (is.na(rylab.at)) 
        rylab.at <- mean(rytickpos)
    if (!is.na(rylab.at)) 
        rylab.at <- rylab.at * ymult + yoff
    color.axis(4, at = axat, labels = rylabels, axlab = rylab, #LINE 123
        axlab.at = rylab.at, col = "black", #axlab.at = rylab.at, col = ifelse(is.na(rcol), 1, rcol), 
        cex.axis = axislab.cex, cex = axislab.cex) #LINE 125
    par(mar = oldmar, new = FALSE, col.axis = "black")
}


来源:https://stackoverflow.com/questions/42558470/handling-axis-with-dates-in-twoord-plot-remove-axis-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!