あなたが表明している懸念は、plot.default
xy.coords に渡されるオブジェクトの制限を処理する方法が嫌いなことです。むしろ、それらが制限またはサブセット化されることを望みます。これらの追加機能を使用して新しいプロット関数を定義することで、これを行うことができます。
# need a helper function for this
tweak <- function(x) c(range(x)[1], range(x)[2]+.00001)
# Replace xy <- xy.coords(x, y, xlabel, ylabel, log)
xy <- xy.coords(x[findInterval(x, tweak(xlim))==1],
y[findInterval(x, tweak(xlim))==1],
xlabel, ylabel, log)
ylimits を保持したい場合は、呼び出しまたはコードで指定する必要があります。私の好みは電話で行うことですが、自動的に行われるように説明しています。
plotsub <- function(x,y = NULL, type = "p", xlim = NULL, ylim = NULL,
log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
ann = par("ann"), axes = TRUE, frame.plot = axes, panel.first = NULL,
panel.last = NULL, asp = NA, ...)
{ ylim=range(y); tweak <- function(x) c(range(x)[1], range(x)[2]+.00001)
localAxis <- function(..., col, bg, pch, cex, lty, lwd) Axis(...)
localBox <- function(..., col, bg, pch, cex, lty, lwd) box(...)
localWindow <- function(..., col, bg, pch, cex, lty, lwd) plot.window(...)
localTitle <- function(..., col, bg, pch, cex, lty, lwd) title(...)
xlabel <- if (!missing(x))
deparse(substitute(x))
ylabel <- if (!missing(y))
deparse(substitute(y))
xy <- xy.coords(x[findInterval(x, tweak(xlim))==1], y[findInterval(x,tweak(xlim))==1], xlabel, ylabel, log)
xlab <- if (is.null(xlab))
xy$xlab
else xlab
ylab <- if (is.null(ylab))
xy$ylab
else ylab
xlim <- if (is.null(xlim))
range(xy$x[is.finite(xy$x)])
else xlim
ylim <- if (is.null(ylim))
range(xy$y[is.finite(xy$y)])
else ylim
dev.hold()
on.exit(dev.flush())
plot.new()
localWindow(xlim, ylim, log, asp, ...)
panel.first
plot.xy(xy, type, ...)
panel.last
if (axes) {
localAxis(if (is.null(y))
xy$x
else x, side = 1, ...)
localAxis(if (is.null(y))
x
else y, side = 2, ...)
}
if (frame.plot)
localBox(...)
if (ann)
localTitle(main = main, sub = sub, xlab = xlab, ylab = ylab,
...)
invisible()
}
呼び出し:
plotsub(1:10, 1:10, type = "l", xlim = c(1, 5), ylim=c(1,10) )
