Following up re personal email on adding graphical parameters that can be passed to grid.lines in hexVP.loess function. Your request: "Can you propose a way that will not break user scripts that use col = 'color' argument?"
Below are a couple of suggestions that may be suitable (I have abbreviated the function calls for brevity but in each case gp would be passed to grid.lines):
We could allow the other graphical parameters to be passed by name in the ellipses
library(grid)
f1a <- function (hbin, hvp = NULL, span = 0.4, col="red", n = 200, ...) {
dots <- list(...)
gp <- do.call(gpar, c(col=col, dots))
return(gp)
}
f1a(); f1a(lwd=2, lty="dashed")
We could probably drop the explicit col parameter above and set the default in the function. This would still allow for col= parameters to be passed without breaking existing code.
We could retain col and use a list element gpar for further parameters. The col parameter only is used to assign colour i.e. overwrites if col given in gpar
f2 <- function (hbin, hvp = NULL, span = 0.4, col="red", n = 200, gp=gpar()) {
gp <- modifyList(gp, list(col=col))
return(gp)
}
f2(); f2(gp=gpar(lwd=2, lty="dashed"))
We could retain col and use a list element gpar for further parameters. If colour parameter col not provided in gpar then use col="red" as default. If col also given in gpar then this takes precedent.
f3 = function (hbin, hvp = NULL, span = 0.4, col="red", n = 200, gp=gpar()) {
if(is.null(gp$col)) gp$col <- col
return(gp)
}
f3(); f3(gp=gpar(lwd=2, lty="dashed"))
I think f1b() is perhaps most consistent with present code. Can document that all graphical parameters can be passed by name in the ellipses, it removes the explicit col= parameter and hopefully shouldn't break existing code. Let me know if any of these are suitable and I'll send a pull request.
A full example using f1b() would be
hexVP.loess_new <- function (hbin, hvp = NULL, span = 0.4, n = 200, ...) {
dots <- list(...)
if(is.null(dots$col)) dots$col <- "red"
gp <- do.call(gpar, dots)
fit <- loess(hbin@ycm ~ hbin@xcm, weights = hbin@count, span = span)
if (!is.null(hvp)) {
pushHexport(hvp, clip = "on")
grid.lines(seq(hbin@xbnds[1], hbin@xbnds[2], length = n),
predict(fit, seq(hbin@xbnds[1], hbin@xbnds[2], length = n)),
gp = gp, default.units = "native")
popViewport()
}
invisible(fit)
}
library(hexbin)
library(grid)
# Some data
d <- data.frame(x=rnorm(1000), y=rnorm(1000))
bin <- hexbin(d$x, d$y)
# old
p <- plot(bin)
hexVP.loess(bin, hvp = p$plot.vp, span = 0.4, col = "red", n = 200)
# new
p <- plot(bin)
hexVP.loess_new(bin, hvp = p$plot.vp, span = 0.4, n = 200)
hexVP.loess_new(bin, hvp = p$plot.vp, span = 0.4, col = "blue", lwd=3, lty="dashed", n = 200)
Hi Edzer,
Following up re personal email on adding graphical parameters that can be passed to
grid.lines
inhexVP.loess
function. Your request: "Can you propose a way that will not break user scripts that use col = 'color' argument?"Below are a couple of suggestions that may be suitable (I have abbreviated the function calls for brevity but in each case
gp
would be passed togrid.lines
):col
parameter above and set the default in the function. This would still allow forcol=
parameters to be passed without breaking existing code.col
and use a list elementgpar
for further parameters. Thecol
parameter only is used to assign colour i.e. overwrites if col given ingpar
col
and use a list elementgpar
for further parameters. If colour parametercol
not provided ingpar
then usecol="red"
as default. Ifcol
also given ingpar
then this takes precedent.I think
f1b()
is perhaps most consistent with present code. Can document that all graphical parameters can be passed by name in the ellipses, it removes the explicitcol=
parameter and hopefully shouldn't break existing code. Let me know if any of these are suitable and I'll send a pull request.A full example using
f1b()
would be