Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 处理twood.plot中带有日期的轴(删除轴3)_R_Date_Plot_Plotrix - Fatal编程技术网

R 处理twood.plot中带有日期的轴(删除轴3)

R 处理twood.plot中带有日期的轴(删除轴3),r,date,plot,plotrix,R,Date,Plot,Plotrix,我试图使用twoord.plot在x轴上绘制不同的日期,ly的计数和ry的百分比,我想删除轴3或更改其颜色。设置轴=F不起作用,xaxt='n'也不起作用 事实上,axes=FALSE给了我这个错误信息: 绘图时出错。默认值(lx,ly,xlim=xlim,ylim=lylim,xlab=xlab,: 形式参数“轴”与多个实际参数匹配 这里给出的解决方案是: 使用不同的rx和lx对我不起作用 下面是我正在使用的类似代码: set.seed(123) library(plotrix) twoord

我试图使用
twoord.plot
在x轴上绘制不同的日期,
ly
的计数和
ry
的百分比,我想删除轴3或更改其颜色。设置轴=F不起作用,xaxt='n'也不起作用

事实上,
axes=FALSE
给了我这个错误信息:

绘图时出错。默认值(lx,ly,xlim=xlim,ylim=lylim,xlab=xlab,: 形式参数
“轴”
与多个实际参数匹配

这里给出的解决方案是:

使用不同的
rx
lx
对我不起作用

下面是我正在使用的类似代码:

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"))
有什么想法吗?提前谢谢!:)

只要破解
twoord.plot
函数就行了。下面是一个使所有轴标签
为黑色的示例(创建绘图时使用
twoord.plot2
而不是
twoord.plot
)。如果要完全删除轴,只需在第88-90行(对于ly)和第123-125行(对于ry)之前添加注释(在行之前添加
#
)。我在下面的函数中为他们留下了注释

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")
}
twoord.plot2=函数(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,…)
{
如果(!为.null(数据)){
ly