2016-04-06 4 views
0

私はrcdimpleを使用して、カテゴリ型の列に基づいてファセット付きの棒グラフを作成しています。プロットは期待どおりに出ていますが、各サブプロットにラベルを適用する方法を理解することはできません。私はいくつかのオプションをコメントアウトしている以下の例では rcdimpleファセット関数のサブプロットラベル

は、私が試してみました:

plt$x$options$tasks <- list(htmlwidgets::JS(' 
    function(){ 
     //this.widgetDimple should hold our chart 
     var chart1 = this.widgetDimple[0]; 
     var chart2 = this.widgetDimple[1]; 
     chart1.svg.append("text") 
     .attr("x", chart1.axes[0]._scale(3)) 
     .attr("y", chart1.axes[1]._scale(300)) 
     .attr("text-anchor", "middle") 
     .text("A Category") 
     chart2.svg.append("text") 
     .attr("x", chart2.axes[0]._scale(3)) 
     .attr("y", chart2.axes[1]._scale(300)) 
     .attr("dy", "0.6em") 
     .attr("text-anchor", "middle") 
     .text("All Others") 
     } 
     ')) 
plt 

fake.data <- read.table(sep=',', header=T, text=" 
    category,variable,value,count 
    A Category,SITE.ACTIVITIES,1,51 
    A Category,SITE.ACTIVITIES,2,116 
    A Category,SITE.ACTIVITIES,3,46 
    A Category,PROXIMITY.TO.RECEPTORS,1,17 
    A Category,PROXIMITY.TO.RECEPTORS,2,111 
    A Category,PROXIMITY.TO.RECEPTORS,3,93 
    All Others,SITE.ACTIVITIES,1,60 
    All Others,SITE.ACTIVITIES,2,37 
    All Others,SITE.ACTIVITIES,3,54 
    All Others,PROXIMITY.TO.RECEPTORS,1,80 
    All Others,PROXIMITY.TO.RECEPTORS,2,167 
    All Others,PROXIMITY.TO.RECEPTORS,3,120 
    ") 



plt <- fake.data %>% 
    dimple(x ="value", y = "count", 
      #title = c('A Category','All Others'), 
      groups = 'category', type = "bar", 
      width = 900, height = 220) %>% 
     facet('variable', 
       #title = c('A Category','All Others'), 
       removeAxes = T) %>% 
     default_colors(c('blue','grey')) %>% 
     xAxis(type = "addCategoryAxis", 
       #facet.title = c('A Category','All Others'), 
       orderRule = "value") %>% 
     yAxis(overrideMax=300, ticks=4) %>% 
     add_legend() %>% 
     add_title(text = c('A Category','All Others')) 

enter image description here

私は、次の追加したthisブログ記事に図2.14を見た後

私は正しい道のりにいると思うが、これを行うにはもっとクリーンな方法があると思う(申し訳ありませんが、私のjavascriptは素晴らしい)。

enter image description here

答えて

0

最も簡単な解決策は、上で概説したようsvg.append("text")を経由してテキストを追加することのようです。 rcdimpleファセット関数は、各サブプロットごとに1つのチャートオブジェクトの配列を作成します。各サブプロットにはOBJECT.data[0].variableでアクセス可能な各ラベルに必要な情報が含まれています。

以下に示す解決策は、任意の数のファセットチャートオブジェクトに対して機能します。数字1および350は、xおよびy軸値に関連するラベルのxおよびy位置に関連します。これらは、よりエレガントな解決策があるかもしれません

plt <- fake.data %>% 
     dimple(x ="value", y = "count", 
       groups = 'category', type = "bar", 
       width = 900, height = 220) %>% 
      facet('variable',removeAxes = T) %>% 
      default_colors(c('blue','grey')) %>% 
      xAxis(type = "addCategoryAxis",orderRule = "value") %>% 
      yAxis(overrideMax=300, ticks=4) %>% 
      add_legend() %>% 
      add_title(text = 'Plot Title') 


plt$x$options$tasks <- list(htmlwidgets::JS(sprintf(' 
    function(){ 
     var n = this.widgetDimple.length 
     var variables = {}; 
     var subs = []; 
     for (var i = 1; i <= n; ++i) subs.push("c"+i) 
     for(var i = 0; i < n; i++) { 
      var v = subs[i]; 
      variables[v] = this.widgetDimple[i] 
      variables[v].svg.append("text") 
      .attr("x", variables[v].axes[0]._scale(%s)) 
      .attr("y", variables[v].axes[1]._scale(%s)) 
      .attr("text-anchor", "left") 
      .text(variables[v].data[0].variable) 
     }; 
    } 
    ', 1, 350))) 
plt 

異なるデータセットのために変更する必要があるであろう、私のJSは素晴らしいではありません。 rcdimpleパッケージの作者とhere

enter image description here

与えられた例のおかげで
関連する問題