首页 > 解决方案 > 使用自己的几何扩展 ggplot:调整默认比例

问题描述

背景

在阅读了这个关于如何扩展 ggplot和我试图理解的相应小插图的ggplot漂亮答案之后,如何扩展.

简而言之

我明白,这些部分是如何组合在一起的,但我错过了一个重要信息:如何ggplot确定轴的默认范围?

代码

考虑以下玩具示例:

library(grid)
library(ggplot2)

GeomFit <- ggproto("GeomFit", GeomBar,
                   required_aes = c("x", "y"),
                   setup_data = .subset2(GeomBar, "setup_data"),
                   draw_panel = function(self, data, panel_scales, coord, width = NULL) {
                     bars <- ggproto_parent(GeomBar, self)$draw_panel(data,
                                                                      panel_scales, 
                                                                      coord)
                     coords <- coord$transform(data, panel_scales)    
                     tg <- textGrob("test", coords$x, coords$y * 2 - coords$ymin)
                     grobTree(bars, tg)
                   }
)

geom_fit <- function(mapping = NULL, data = NULL,
                     stat = "count", position = "stack",
                     ...,
                     width = NULL,
                     binwidth = NULL,
                     na.rm = FALSE,
                     show.legend = NA,
                     inherit.aes = TRUE) {

  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomFit,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      width = width,
      na.rm = na.rm,
      ...
    )
  )
}

set.seed(1234567)
data_gd <- data.frame(x = letters[1:5], 
                      y = 1:5)

p <- ggplot(data = data_gd, aes(x = x, y = y, fill = x)) + 
  geom_fit(stat = "identity")

这产生了这个情节: 带文本的条形图

问题

如您所见,某些文本未显示。我假设以ggplot某种方式计算轴的范围,因为它不知道我的textGrob. 我该如何解决?(期望的结果相当于p + expand_limits(y = 10)

注意。当然,我可以通过要求添加手动比例将问题推给最终用户。但理想情况下,我希望正确设置秤。

标签: rggplot2ggproto

解决方案


尤里卡

感谢@Z.Lin 在如何调试ggplot代码方面的出色帮助,发现了一个丑陋的 hack。以下是我如何想出这个相当丑陋的 hack 以供将来参考:

我是怎么到那里的

在调试debug(ggplot2:::ggplot_build.ggplot)时,我了解到罪魁祸首可以在某个地方找到FacetNull$train_scales(在我没有方面的情节中,其他方面就像FacetGrid以类似的方式工作)。我从中学到了debug(environment(FacetNull$train_scales)$f)这一点,而我又在另一个线程中从 Z.Lin 的答案中学到了这一点。

一旦我能够调试ggproto对象,我就可以看到在这个函数中训练了尺度。基本上,该功能着眼于与特定比例相关的美学(不知道首先在哪里设置此信息,任何想法?)并查看这些美学中的哪些存在于图层数据中。

我看到一个字段ymax_final(根据此表,仅用于stat_boxplot)是考虑设置比例的字段之一。有了这条信息,通过将此字段设置setup_data为适当的值,很容易找到一个丑陋的 hack。

代码

GeomFit <- ggproto("GeomFit", GeomBar,
                   required_aes = c("x", "y"),
                   setup_data = function(self, data, params) {
                      data <- ggproto_parent(GeomBar, self)$setup_data(data, params)
                      ## here's the hack: add a field which is not needed in this geom, 
                      ## but which is used by Facet*$train_scales which 
                      ## eventually sets up the scales
                      data$ymax_final <- 2 * data$y
                      data
                   }, 
                   draw_panel = function(self, data, panel_scales, coord, width = NULL) {
                     bars <- ggproto_parent(GeomBar, self)$draw_panel(data,
                                                                      panel_scales, 
                                                                      coord)
                     coords <- coord$transform(data, panel_scales)    
                     tg <- textGrob("test", coords$x, coords$y * 2 - coords$ymin)
                     grobTree(bars, tg)
                   }
)

结果

带标签的条形图

开放式问题

如果您不手动定义(轴)刻度,它们在哪里设置?我猜那里设置了与缩放相关的字段。


推荐阅读