我正在苦苦掙扎as.POSIXct.numeric
。以下代碼有效:
dates <- structure(c(1649285787, 1651134684), class = c("POSIXct", "POSIXt"),
tzone = "UTC")
lims <- as.numeric(dates)
limits <- as.POSIXct.numeric(lims, origin = as.POSIXct("1970-01-01", tz = "UTC"))
plot(stepfun(x = dates, # OK
y = c(0, 1, 2)),
do.points = FALSE)
plot(stepfun(x = dates, # OK
y = c(0, 1, 2)),
do.points = FALSE,
xlim = lims)
但是,以下失敗,我不明白為什么(我需要指定限制):
plot(stepfun(x = dates, # Not OK
y = c(0, 1, 2)),
do.points = FALSE,
xlim = dates)
# Error in as.POSIXct.numeric(e) : 'origin' must be supplied
plot(stepfun(x = dates, # Not OK
y = c(0, 1, 2)),
do.points = FALSE,
xlim = limits)
# Error in as.POSIXct.numeric(e) : 'origin' must be supplied
有人理解這種行為嗎?我明確指定了原點as.POSIXct.numeric(lims, origin = ...)
!
uj5u.com熱心網友回復:
第一個塊起作用的原因是因為stepfun(x = dates, y = c(0, 1, 2))
回傳一個類“stepfun”的物件(您可以通過運行來檢查它class(stepfun(x = dates, y = c(0, 1, 2)))
)并且 stepfun 物件的 plot 方法將繪制一個將 X 和 Y 軸設定為數字的圖。換句話說,您的第一個塊有效,因為它xlim
被設定為數字(這是 stepfun 物件的繪圖方法所期望的)。相反,當您xlim
使用POSIX
向量(dates
或limits
)設定時,繪圖方法根本不會對它們做什么,因為它需要一個數字向量。
如果您需要更改軸標簽,我建議在外部進行如下操作:
plot(stepfun(x = dates,
y = c(0, 1, 2)),
do.points = FALSE,
xlim = lims, axes = FALSE)
xLabels <- pretty(limits)
axis(side = 1, at = as.numeric(xLabels), labels = xLabels)
axis(side = 2)
box()
pretty
函式只是找出“漂亮”(四舍五入)斷點向量的一種快速方法,但可以使用另一種方法來設定該值。
uj5u.com熱心網友回復:
我們可能會嘗試plot.stepfun
通過從第 30 行開始做一些日期轉換來破解這個函式。更具體地說,我們將xlim=
in"POSIXt"
格式轉換seq
為日期的影響,并在第 63 行將其用作axis
.
plot_stepfun <- function(x, xval, xlim, ylim=range(c(y, Fn.kn)), xlab="x",
ylab="f(x)", main=NULL, add=FALSE, verticals=TRUE,
do.points=(n < 1000), pch=par("pch"), col=par("col"),
col.points=col, cex.points=par("cex"), col.hor=col,
col.vert=col, lty=par("lty"), lwd=par("lwd"), ...) {
if (!is.stepfun(x)) {
if (is.numeric(x)) {
sarg <- substitute(x)
x <- ecdf(x)
attr(x, "call") <- call("ecdf", sarg)
}
else stop("'plot.stepfun' called with wrong type of argument 'x'")
}
if (missing(main)) {
cl <- attr(x, "call")
main <- if (is.null(cl)) sys.call() else cl
}
knF <- knots(x)
xval <- if (missing(xval))
knF
else sort(xval)
if (missing(xlim)) {
rx <- range(xval)
dr <- if (length(xval) > 1L)
max(0.08 * diff(rx), median(diff(xval)))
else abs(xval)/16
xlim <- rx dr * c(-1, 1)
}
else {
xlim1 <- xlim
xlim <- as.numeric(xlim)
if (inherits(xlim1, c('Date', "POSIXt"))) {
tz <- attributes(as.POSIXlt(xlim1))$tzone
xlim2 <- as.POSIXct(paste(strftime(xlim1, '%F')), tz=tz[length(tz)])
if (as.numeric(difftime(xlim2[2], xlim2[1], units='secs')) < 86400) {
stop('will be implemented soon :)')
} else {
xseq <- seq.POSIXt(xlim2[1], xlim2[2], by='days')
if (length(xseq) > 6) {
xseq <- xseq[(seq_along(xseq) - 5) %% ceiling(length(xseq)/6) == 0]
}
}
}
dr <- diff(xlim)
}
xval <- xval[xlim[1L] - dr <= xval & xval <= xlim[2L] dr]
ti <- c(xlim[1L] - dr, xval, xlim[2L] dr)
ti.l <- ti[-length(ti)]
ti.r <- ti[-1L]
y <- x(0.5 * (ti.l ti.r))
n <- length(y)
Fn.kn <- x(xval)
dev.hold()
on.exit(dev.flush())
if (add)
segments(ti.l, y, ti.r, y, col=col.hor, lty=lty,
lwd=lwd, ...)
else {
if (missing(ylim))
ylim <- range(c(y, Fn.kn))
plot(NA, NA, type="n", xlim=xlim, ylim=ylim, xlab=xlab,
ylab=ylab, main=main, xaxt='n', ...)
if (exists('xseq')) {
axis(1, xseq, xseq)
} else {
axis(1, at=axTicks(1), labels=axTicks(1))
}
segments(ti.l, y, ti.r, y, col=col.hor, lty=lty,
lwd=lwd)
}
if (do.points)
points(xval, Fn.kn, pch=pch, col=col.points, cex=cex.points)
if (verticals)
segments(xval, y[-n], xval, y[-1L], col=col.vert, lty=lty,
lwd=lwd)
invisible(list(t=ti, y=y))
}
plot_stepfun(stepfun(x=dates, y=c(0, 1, 2)), do.points=FALSE) ## works!
plot_stepfun(stepfun(x=dates, y=c(0, 1, 2)), do.points=FALSE, xlim=lims) ## works!
plot_stepfun(stepfun(x=dates, y=c(0, 1, 2)), do.points=FALSE, xlim=dates) ## works!
plot_stepfun(stepfun(x=dates, y=c(0, 1, 2)), do.points=FALSE, xlim=limits) ## works!
資料:
dates <- structure(c(1649285787, 1651134684), class = c("POSIXct", "POSIXt"
), tzone = "UTC")
lims <- as.numeric(dates)
limits <- as.POSIXct.numeric(lims, origin=as.POSIXct("1970-01-01", tz="UTC"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/496841.html
上一篇:如何在php中轉換日期
下一篇:修復多個div中的可拖動位置