5

ローソク足チャートのようなものである geom_ohlc() という新しい Geom タイプを作成して、株価の始値 - 高値 - 安値 - 終値データをプロットします。

このハドリーの記事を学んだ後:私はこれを試しました:

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

簡単にするために、バーの色は考慮していません。

結果プロットは次のようになります。

スクリーンショット

browser()関数内に a を追加しましたが、 が, ,の美学を interverl [0,1] に変換しggprotoていないことがわかりました。この問題を解決するには?coord$transformophilocl

さらに、Hadley の記事以外に、独自の Geom タイプを作成する方法に関するドキュメントはありますか?

4

1 に答える 1

2

OPの質問の下のコメントで述べたように、問題はaes_to_scale()内部の関数transform_position()であり、それはによって呼び出されcoord$transformます。変換は、指定された変数とx, xmin, xmax, xend, xintercepty 軸に相当するものに限定されます。これは、transform_position のヘルプに記載されています。

説明

すべての位置変数を変換する便利な関数。

使用法

transform_position(df, trans_x = NULL, trans_y = NULL, ...) 引数

trans_x、trans_y x および y 美学のための変換関数。(x、xmin、xmax、xend などを変換します) ... trans_x と 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 で任意のスケール変数名をサポートする必要があるのでしょうか?

于 2016-10-16T15:19:49.510 に答える