给定以下基于 ggplot 的环境(最初来自:ggplot2: Multiple color scales or shift colours systemly on different layers?)
GeomBoxplotDark <- ggproto(ggplot2:::GeomBoxplot,expr={
draw <- function(., data, ..., outlier.colour = "black", outlier.shape = 16, outlier.size = 2) {
defaults <- with(data, { # ** OPENING "{" ADDED **
cols_dk <- rgb2hsv(col2rgb(colour)) - c(0, 0, 0.2) # ** LINE ADDED **
cols_dk <- hsv(cols_dk[1,], cols_dk[2,], cols_dk[3,]) # ** LINE ADDED **
data.frame(x = x, xmin = xmin, xmax = xmax,
colour = cols_dk, # ** EDITED, PASSING IN cols_dk **
size = size,
linetype = 1, group = 1, alpha = 1,
fill = alpha(fill, alpha),
stringsAsFactors = FALSE
)}) # ** CLOSING "}" ADDED **
defaults2 <- defaults[c(1,1), ]
if (!is.null(data$outliers) && length(data$outliers[[1]] >= 1)) {
outliers_grob <- with(data,
GeomPoint$draw(data.frame(
y = outliers[[1]], x = x[rep(1, length(outliers[[1]]))],
colour=I(outlier.colour), shape = outlier.shape, alpha = 1,
size = outlier.size, fill = NA), ...
)
)
} else {
outliers_grob <- NULL
}
with(data, ggname(.$my_name(), grobTree(
outliers_grob,
GeomPath$draw(data.frame(y=c(upper, ymax), defaults2), ...),
GeomPath$draw(data.frame(y=c(lower, ymin), defaults2), ...),
GeomRect$draw(data.frame(ymax = upper, ymin = lower, defaults), ...),
GeomRect$draw(data.frame(ymax = middle, ymin = middle, defaults), ...)
)))
}
}
)
我想用以下函数调用它:
geom_boxplot_dark <- function (mapping = NULL, data = NULL, stat = "boxplot", position = "dodge",
outlier.colour = "black", outlier.shape = 16, outlier.size = 2,
...)
GeomBoxplotDark$new(mapping = mapping, data = data, stat = stat,
position = position, outlier.colour = outlier.colour, outlier.shape = outlier.shape,
outlier.size = outlier.size, ...)
但是,在调用该函数时,它无法正确调用环境并给出以下错误:
> geom_boxplot_dark()
Error in geom_boxplot_dark() : attempt to apply non-function
有什么建议么?