1

Given the following ggplot-based environment (originally from: ggplot2: Multiple color scales or shift colors systematically 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), ...)
                       )))
                     }
  }
)

I want to call it with the following function:

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

However, when calling the function it fails to call the environment properly and gives the following error:

> geom_boxplot_dark()
Error in geom_boxplot_dark() : attempt to apply non-function

Any suggestions?

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
Borja
  • 63
  • 2
  • 6
  • Have you checked out [this small vignette](http://docs.ggplot2.org/current/vignettes/extending-ggplot2.html) by the package creator? If you're looking to make a modification to the boxplot, there might be an easier way. – nograpes Apr 04 '16 at 21:01
  • But, having read that document, it looks like there are many problems here: your second argument to `ggproto` should be `Geom`, and the third argument should not be an expression, but `draw = function(...`. You can't use `<-` here because the function needs to be input as an argument to the `ggproto` function. – nograpes Apr 04 '16 at 21:04
  • Thanks for the feedback. I edited the environment as you suggested, but still the same error remains when calling geom_boxplot_dark function. It seems that the enviroment is well created, but not well called by the function. – Borja Apr 04 '16 at 21:44
  • I edited the function geom_boxplot_dark(), and this seems to work: `geom_boxplot_dark <- function (mapping = NULL, data = NULL, stat = "boxplot", position = "dodge", ..., outlier.colour = "black", outlier.color = NULL, outlier.shape = 16, outlier.size = 0.2, outlier.stroke = 0.5) { layer(data = data, mapping = mapping, stat = stat, geom = GeomBoxplot, position = position, show.legend = show.legend, inherit.aes = inherit.aes, params = list(outlier.colour = outlier.colour, outlier.shape = outlier.shape, outlier.size = outlier.size, outlier.stroke = outlier.stroke, ...)) }` – Borja Apr 06 '16 at 09:10

0 Answers0