r语言 - 从原型移动到 ggproto 以创建环境



我正在使用 ggplot2 中的 GeomBoxplotDark 环境:在不同图层上系统地转换多个色阶或系统地转换颜色?

然而,ggplot2 从 proto 转移到 ggproto,所以这不再有效了。

具体代码为:

require(ggplot2)
GeomBoxplotDark <- proto(ggplot2:::GeomBoxplot,
  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), ...)
)))
  }
) 

它给出以下错误: _inherit必须是 ggproto 对象。

我的问题是:如何解决此错误,以便可以使用ggproto创建环境?提前非常感谢。

以下内容对我有用。我添加了expr={}包装器。

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

相关内容

  • 没有找到相关文章

最新更新