如何更改R代码中图例的位置?

but5z9lq  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(115)
library(robfilter)
res <- robust.filter(cow,width=7)
plot(res)
ind <- which(res$ol!=0)
points(ind,cow[ind])

我希望图例在左上角,但当我编写图例命令时,它会绘制第二个图例,但我无法删除自动绘制的图例。
每一个帮助是非常感谢!谢谢!

ymzxtsji

ymzxtsji1#

robfilter:::plot.robust.filter函数正在进行绘图,它具有"topright"硬编码。您必须编写自己的函数来覆盖它。例如:

plot.robust.filter <- function (x, legend = "topright", ...) {
    N <- length(x$y)
    ylims <- c(min(x$y, min(x$level, na.rm = TRUE), na.rm = TRUE), 
        max(x$y, max(x$level, na.rm = TRUE), na.rm = TRUE))
    xlims <- c(1, N)
    if (x$online) {
        ol.text <- "Online "
    }
    else {
        ol.text <- ""
    }
    if (x$adapt == 0) {
        adapt.text <- paste(" with Window Width ", x$width, sep = "")
    }
    else {
        adapt.text <- " with Adaptive Window Width"
    }
    titel <- paste(ol.text, x$trend, " ", x$outlier, "-", x$scale, 
        " Filter", adapt.text, sep = "")
    plot(x$y, type = "l", main = titel, ylim = ylims, xlim = xlims, 
        xlab = "Time", ylab = x$ts.name)
    lines(x$level, col = "red", lty = 1, lwd = 2)
    legend(x = legend, bty = "n", legend = c("Time Series", 
        "Filtered Signal"), lty = c(1, 1), col = c("black", "red"), 
        lwd = c(1, 2))
}

此版本默认与原始函数位于相同的位置,但允许您将其作为

plot(res, legend = "topleft")

来改变位置。

相关问题