r - 在 ggproto 中,coord$transform 没有将某些列转换为 [0, 1]

标签 r ggplot2 ggproto

我想创建一个新的 Geom 类型:geom_ohlc(),它类似于烛台图,用于绘制股票开盘-高-低-收盘数据。

学完这个Hadley's article :我试过这个:

GeomOHLC <- ggproto(`_class` = "GeomOHLC", `_inherit` = Geom,

                 required_aes = c("x", "op", "hi", "lo", "cl"),

                 draw_panel = function(data, panel_scales, coord){

                   coords <- coord$transform(data, panel_scales)
                   browser() # <<-- here is where I found the problem
                   grid::gList(
                     grid::rectGrob(
                       x = coords$x,
                       y = pmin(coords$op, coords$cl),
                       vjust = 0,
                       width = 0.01,
                       height = abs(coords$op - coords$cl),
                       gp = grid::gpar(col = coords$color, fill = "yellow")
                     ),
                     grid::segmentsGrob(
                       x0 = coords$x,
                       y0 = coords$lo,
                       x1 = coords$x,
                       y1 = coords$hi
                       )
                   )
                 })
    geom_ohlc <- function(data = NULL, mapping = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...)
    {
      layer(
        geom = GeomOHLC, mapping = mapping, data = data, 
        stat = stat, position = position, show.legend = show.legend, 
        inherit.aes = inherit.aes, params = list(na.rm = na.rm, ...)
      )
    }   
    dt <- data.table(x = 1:10, open = 1:10, high = 3:12, low = 0:9, close = 2:11)
    p <- ggplot(dt, aes(x = x, op = open, hi = high, lo = low, cl = close)) + 
      geom_ohlc() 
    p

为了简单起见,我只是不考虑条形的颜色。

结果图是这样的:

screenshot

我在ggproto函数中添加了browser(),我发现coord$transform没有转换 ophilocl 美学融入 interverl [0,1]。如何解决这个问题?

此外,除了Hadley的文章之外,还有其他关于如何创建自己的Geom类型的文档吗?

最佳答案

正如OP问题下的评论中所提到的,问题是transform_position()内的aes_to_scale()函数,该函数又由coord$transform调用。转换仅限于名为 x、xmin、xmax、xend、xintercept 的变量以及 y 轴的等效变量。在transform_position的帮助中提到了这一点:

Description

Convenience function to transform all position variables.

Usage

transform_position(df, trans_x = NULL, trans_y = NULL, ...) Arguments

trans_x, trans_y Transformation functions for x and y aesthetics. (will transform x, xmin, xmax, xend etc) ... Additional arguments passed to trans_x and trans_y.

解决方法是使用这些变量名称而不是 OP 使用的变量名称。以下代码可用于转换变量,但在其他地方失败(请参阅末尾)。我不知道预期情节的细节,因此没有尝试修复此错误。

GeomOHLC <- ggproto(
  `_class` = "GeomOHLC",
  `_inherit` = Geom,

  required_aes = c("x", "yintercept", "ymin", "ymax", "yend"),

  draw_panel = function(data, panel_scales, coord) {
    coords <- coord$transform(data, panel_scales)
    #browser() # <<-- here is where I found the problem
    grid::gList(
      grid::rectGrob(
        x = coords$x,
        y = pmin(coords$yintercept, coords$yend),
        vjust = 0,
        width = 0.01,
        height = abs(coords$op - coords$cl),
        gp = grid::gpar(col = coords$color, fill = "yellow")
      ),
      grid::segmentsGrob(
        x0 = coords$x,
        y0 = coords$ymin,
        x1 = coords$x,
        y1 = coords$ymax
      )
    )
  }
)
geom_ohlc <-
  function(data = NULL,
           mapping = NULL,
           stat = "identity",
           position = "identity",
           na.rm = FALSE,
           show.legend = NA,
           inherit.aes = TRUE,
           ...)
  {
    layer(
      geom = GeomOHLC,
      mapping = mapping,
      data = data,
      stat = stat,
      position = position,
      show.legend = show.legend,
      inherit.aes = inherit.aes,
      params = list(na.rm = na.rm, ...)
    )
  }
dt <-
  data.table(
    x = 1:10,
    open = 1:10,
    high = 3:12,
    low = 0:9,
    close = 2:11
  )
p <-
  ggplot(dt, aes(
    x = x,
    yintercept = open,
    ymin = high,
    ymax = low,
    yend = close
  )) +
  geom_ohlc()
p

这会转换变量,但会产生以下错误:

Error in unit(height, default.units) : 
  'x' and 'units' must have length > 0 

但希望从这里开始它可以发挥作用。

注意:我相当随意地选择了原始变量名称(op、hi、lo、cl)之间的映射。特别是yintercept似乎不太适合。也许ggplot2需要支持任意比例变量名称?

关于r - 在 ggproto 中,coord$transform 没有将某些列转换为 [0, 1],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40068502/

相关文章:

r - R中的双聚类

r - R图中的不均匀轴

r - ggplot2中的饼图具有可变的饼图大小

r - 在ggplot中结合条形图和散点图

r - 在geom_boxplot中更改晶须定义

减少同一列中的因子水平

r - 转换数据框

r - 如何在图例中显示geom_abline的abline

r - 如何手动调整刻面大小

r - 如何在ggplot中使用图像作为点?