如何手动建立ggplot2色阶?

时间:2019-06-12 14:52:32

标签: r 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 $”中)如何获取自定义变量并将其转换为一定比例?

0 个答案:

没有答案