rcdimple facet 函数子图标签

rcdimple facet function subplot labels

我正在使用 rcdimple 创建一组基于分类列的分面条形图。这些图按预期出现,但我无法弄清楚如何将标签应用于每个子图。

在下面的示例中,我已经注释掉了我尝试过的一些选项:

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'))

this 博客 post 中看到图 2.14 后,我添加了以下内容:

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

我认为我在正确的道路上,但我认为可能有更简洁的方法来做到这一点(抱歉,我的 javascript 不是很好)。

最简单的解决方案似乎是通过 svg.append("text") 添加文本,如上所述。 rcdimple facet 函数为每个子图创建一个图表对象数组。反过来,每个子图包含每个标签所需的信息,可通过 OBJECT.data[0].variable 访问。

下面介绍的解决方案适用于任意数量的分面图对象。数字 1350 涉及与 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 package and the examples given here

的作者