首页 > 解决方案 > 如何手动构建ggplot2色标?

问题描述

想象一下,我们更换StatIdentityGeomPoint像这样:

StatIdentity2 <- ggproto("StatIdentity2", Stat,
                        compute_layer = function(self, data, params, layout) {
                          ddx <<- data
                          data
                        }
)


stat_identity <- function(mapping = NULL, data = NULL,
                          geom = "point2", position = "identity",
                          ...,
                          show.legend = NA,
                          inherit.aes = TRUE) {
  layer(
    data = data,
    mapping = mapping,
    stat = StatIdentity2,
    geom = geom,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      na.rm = FALSE,
      ...
    )
  )
}

geom_point2 <- function(mapping = NULL, data = NULL,
                       stat = "identity2", position = "identity",
                       ...,
                       na.rm = FALSE,
                       show.legend = NA,
                       inherit.aes = TRUE) {
  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomPoint2,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      na.rm = na.rm,
      ...
    )
  )
}

GeomPoint2 <- ggproto("GeomPoint2", Geom,
                     required_aes = c("x", "y"),
                     non_missing_aes = c("size", "shape", "colour"),
                     default_aes = aes(
                       shape = 19, colour = "black", size = 1.5, fill = NA,
                       alpha = NA, stroke = 0.5
                     ),

                     draw_panel = function(data, panel_params, coord, na.rm = FALSE) {
                       if (is.character(data$shape)) {
                         data$shape <- translate_shape_string(data$shape)
                       }
                       ddy <<- data
                       ppy <<- panel_params
                       ccy <<- coord

                       coords <- coord$transform(data, panel_params)
                       ggplot2:::ggname("geom_point",
                              pointsGrob(
                                coords$x, coords$y,
                                pch = coords$shape,
                                gp = gpar(
                                  col = alpha(coords$colour, coords$alpha),
                                  fill = alpha(coords$fill, coords$alpha),
                                  # Stroke is added around the outside of the point
                                  fontsize = coords$size * .pt + coords$stroke * .stroke / 2,
                                  lwd = coords$stroke * .stroke / 2
                                )
                              )
                       )
                     },

                     draw_key = draw_key_point
)

然后我们看到它StatIdentity传递了我们为color变量分配的原始值。但是中的值GeomPoint被转换为色标。这种转变究竟发生在哪里?(我假设在 `layout$get_scales$) 我怎样才能获取一些自定义变量并将其转换为某种比例?

标签: rggplot2

解决方案


推荐阅读