2016-10-16 8 views
5

में परिवर्तित नहीं किया है, मैं एक नया जियोम प्रकार बनाना चाहता हूं: 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

मैं एक browser() अंदर ggproto समारोह जोड़ने के लिए, और मैंने पाया कि coord$transform interverl में op, hi, lo, cl सौंदर्यशास्त्र को बदलने नहीं हुआ [ 0,1]। इस समस्या से कैसे निपटा जाए ?

इसके अलावा, हैडली के लेख को छोड़कर अपने स्वयं के जियोम प्रकार को बनाने के तरीके के बारे में कोई अन्य दस्तावेज है?

+0

दोष 'रूपांतरण $ परिवर्तन' में फ़ंक्शन द्वारा बुलाया गया 'transform_position()' लगता है। कोड दिखाता है कि यह केवल 'x' और 'y' नामक डेटा.फ्रेम में कॉलम को बदल देगा। मुझे आपकी समस्या का हल पता नहीं है, लेकिन मैं यह सोचने की कोशिश करता हूं कि आपको * * को पुनः प्राप्त करने की आवश्यकता है (?)। – ddiez

+0

हालांकि दूसरे विचार पर एक ही कोड "एक्स" और/या "वाई" होने वाले कई तराजू की संभावना का भी सुझाव देता है। शायद वहाँ कुछ है? – ddiez

+0

इसके अलावा, 'ggplot2 ::: aes_to_scale' फ़ंक्शन' data.frame' नामों को मैपिंग कर रहा है, चाहे वे 'x' या' y' स्केल हों। दस्तावेज को देखकर आपको 'x "," xmin "," xmax "," xend "," xintercept "या y अक्ष के समकक्ष सौंदर्यशास्त्र का उपयोग करने की आवश्यकता है। इसका उल्लेख '? Transform_position' में भी किया गया है। मुझे लगता है कि यदि आप अपने चर को मैप करने के लिए एक तरीका समझ सकते हैं तो आप अपनी समस्या का समाधान करेंगे। – ddiez

उत्तर

2

ओपी के प्रश्न के तहत टिप्पणियों में उल्लिखित अनुसार समस्या aes_to_scale()transform_position() के अंदर फ़ंक्शन है, जिसे बदले में coord$transform कहा जाता है। परिवर्तन x, xmin, xmax, xend, xintercept नामक चर के लिए सीमित हैं और वाई धुरी के समकक्ष हैं। यह 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.

का संभावित हल ओपी द्वारा इस्तेमाल किया चर नाम के बजाय उन चर नाम का उपयोग करने के लिए होगा। निम्नलिखित कोड चर बदलने में काम करता है लेकिन यह कहीं और विफल रहता है (अंत में देखें)। मैं इच्छित साजिश के विवरण नहीं जानता, इसलिए इस त्रुटि को ठीक करने का प्रयास नहीं किया।

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 

लेकिन यहाँ से उम्मीद है कि यह काम करने के लिए बनाया जा सकता है।

नोट: मैंने मूल परिवर्तनीय नामों (ओप, हाय, लो, सीएल) के बीच मैपिंग को मनमाने ढंग से चुना है। विशेष रूप से yintercept अच्छी तरह से फिट नहीं लग रहा है। शायद ggplot2 में मनमाना पैमाने चर नामों का समर्थन करने की आवश्यकता है?

+0

मुझे लगता है कि आपको बिंदु मिल गया है। मैं 'draw_panel' फ़ंक्शन के अंदर 'डेटा' के कॉलम नामों को 'ओप, हाय, लो, सीएल' से' y, ymax, ymin, yend' में बदलने का प्रयास करता हूं। फिर इन कॉलम को [0, 1] में बदलने के लिए 'coords $ transfrom'" जानता है "। –

संबंधित मुद्दे