簡體   English   中英

子圖內的邊距

[英]margin within a subset plot

當我們選擇整個數據范圍時,會得到一個在繪圖區域內略有空白的繪圖,這樣繪圖就不會觸及邊界框。 但是,當我們對圖進行子集化時,邊距消失了。 有沒有辦法增加這個余量? 在下面的圖中,我希望該行一直上升到5,但不會更遠。 我瀏覽了?par列表,無法提出有用的信息。

plot(1:10, 1:10, type = "l")
plot(1:10, 1:10, type = "l", xlim = c(1, 5))

在此處輸入圖片說明

這似乎是一種非常round回的方式,但是您提供的示例有一種可能:

plot(1:10, 1:10, type = "n", xlim = c(1, 5))
usr = par("usr")
clip(usr[1], 5, usr[3], usr[4])
lines(1:10, 1:10, type = "l", xlim = c(1, 5))

您表達的擔憂是不喜歡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)

如果您打算保留ylimit,則需要在調用或代碼中指定這些值。 我的偏好是在通話中進行操作,但我將其說明為自動完成。

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

在此處輸入圖片說明

您不是要對數據進行子集設置,而是要設置圖的限制。 先驗子集為您提供所需的行為。

plot(1:5, 1:5, type = "l")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM