简体   繁体   English

`geom_abline()` 如何不影响 x 和 y 尺度?

[英]How does `geom_abline()` not affect the x and y scales?

Does anyone know which argument within geom_abline() is responsible for not affecting the x and y scales?有谁知道geom_abline()哪个参数负责不影响 x 和 y 尺度?

The function draw_panel() w/i GeomAbline gets the underlying "ranges", but the line should than be typically lie outside the original scales:函数draw_panel() w/i GeomAbline获取潜在的“范围”,但该线通常应位于原始比例之外:

GeomAbline <- ggproto("GeomAbline", Geom,
  draw_panel = function(data, panel_params, coord) {
    ranges <- coord$backtransform_range(panel_params)

    data$x    <- ranges$x[1]
    data$xend <- ranges$x[2]
    data$y    <- ranges$x[1] * data$slope + data$intercept
    data$yend <- ranges$x[2] * data$slope + data$intercept

    GeomSegment$draw_panel(unique(data), panel_params, coord)
  },

  default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA),
  required_aes = c("slope", "intercept"),

  draw_key = draw_key_abline
)

My guess are the arguments/functions ggplot2::StatIdentity and ggplot2::PositionIdentity set in ggplot2:layer() of geom_abline() .我的猜测是在ggplot2:layer()geom_abline() ggplot2:layer()中设置的参数/函数ggplot2::StatIdentityggplot2::PositionIdentity But I don't understand how this works?但我不明白这是如何工作的? My motivation is to write a new geom_* that also does not affect the x and y scales.我的动机是编写一个新的geom_* ,它也不会影响 x 和 y 尺度。

According to @teunbrand's comment, the solution is the following:根据@teunbrand 的评论,解决方案如下:

Inside ggplot2::scale_x_continuous() only the following aesthetics can affect the x-scaling: c("x", "xmin", "xmax", "xend", "xintercept", "xmin_final", "xmax_final", "xlower", "xmiddle", "xupper", "x0") .ggplot2::scale_x_continuous()只有以下美ggplot2::scale_x_continuous()影响 x 缩放: c("x", "xmin", "xmax", "xend", "xintercept", "xmin_final", "xmax_final", "xlower", "xmiddle", "xupper", "x0") However, since geom_abline() does not contain any of these aesthetics, the scales are not affected.然而,由于geom_abline()不包含任何这些美学,比例不受影响。

The same applies to the y scaling.这同样适用于 y 缩放。 By the way, the variables correspond to the characters in ggplot2:::ggplot_global$x_aes and ggplot2:::ggplot_global$y_aes , but these are not used for reasons unknown to me.顺便说一下,变量对应于ggplot2:::ggplot_global$x_aesggplot2:::ggplot_global$y_aes ,但由于我不知道的原因,这些ggplot2:::ggplot_global$y_aes没有使用。

For illustration I have rewritten geom_point() , but with aesthetics c("x_new", "y") .为了说明,我重写了geom_point() ,但使用了美学c("x_new", "y")

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


GeomMyPoint <- ggplot2::ggproto("GeomMyPoint", ggplot2::GeomPoint,
  required_aes = c("x_new", "y"),
  
  draw_panel = function(data, panel_scales, coord) {
    if (is.character(data$shape)) {
      data$shape <- translate_shape_string(data$shape)
    }

    ## Transform the data first
    coords <- coord$transform(data, panel_scales)

    ## Construct a grid grob
    grid::pointsGrob(
      x = coords$x_new,
      y = coords$y,
      pch = coords$shape,
      gp = grid::gpar(
        col = alpha(coords$colour, coords$alpha),
        fill = alpha(coords$fill, coords$alpha),
        fontsize = coords$size * ggplot2::.pt + coords$stroke * ggplot2::.stroke / 2,
        lwd = coords$stroke * ggplot2::.stroke / 2
      )
    )
  }
)

The output of geom_my_point() with no x aesthetic corresponds to figure "gg2";没有x美学的geom_my_point()的输出对应于图形“gg2”; hence the red points do not effect the x scales:因此红点不会影响 x 尺度:

d <- data.frame(x = runif(200))
d$y <- 1 * d$x + rnorm(200, 0, 0.2)
d$x2 <- d$x * 2

require("ggplot2")
gg1 <- ggplot(d) + geom_point(aes(x, y)) + geom_point(aes(x = x2, y = y), col = 2) + ggtitle("gg1")
gg2 <- ggplot(d) + geom_point(aes(x, y)) + geom_my_point(aes(x_new = x2, y = y), col = 2) + ggtitle("gg2")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM