如何有条件地设置共享数据集的绘图轴范围

How to conditionally set plotly axis range for a shared data set

我已经构建了一个 R flexdashboard,并且正在使用 htmlwidget、crosstalk 来过滤我的仪表板的 plotly 图表。

我当前的问题是水平条形图的数据标签被截断了。我试图通过调整 x 轴最大值来解决这个问题。更具体地说,我正在尝试根据每个过滤组的值调整 x 轴最大值,以便图表为所有组生成适当的轴范围。

我目前正在尝试使用布局(xaxis = list(range = c(~xaxis_max[1]))进行绘图。但是,这索引了整个共享数据中的第一个值集,而不是筛选共享数据集中的第一个值。

关于如何调整以使每个组的图表具有特定于组的轴最大值的任何想法?

下面的完整表示(图表显示为团队 1,轴未调整而不是团队 2):

---
title: "Donut Dashboard"
output: 
  flexdashboard::flex_dashboard
---

```{r setsup, echo=FALSE} 
library(flexdashboard) 
library(plotly) 
library(tidyverse)
library(crosstalk)
``` 

my_tibble <- tibble(donuts_eaten = c(7436, 12761, 13153, 12707, 183, 377, 368, 381),
                    week = c(1,2,3,4,1,2,3,4),
                    donut_type = c("glazed", "chocolate", "cake", "sprinkle", 
                                  "glazed", "chocolate", "cake", "sprinkle"),
                    group = c("team 1", "team 1", "team 1", "team 1", 
                               "team 2", "team 2", "team 2", "team 2"))

#Create group-specific variable for x-axis max
my_tibble <- my_tibble %>% 
  group_by(group) %>% 
  mutate(xaxis_max = 1.5*max(donuts_eaten)) %>% 
  ungroup()

    my_tibble_shared <- SharedData$new(my_tibble, ~group)
    
    ```
    
    Column {data-width=650}
    -----------------------------------------------------------------------
    
    <!-- Create filter for plotly chart -->
    ### Filter
    
    ```{r}
    filter_select(
      id = "group",
      label = "Filter group:",
      sharedData = my_tibble_shared,
      group = ~group,
      multiple = F
    )
    ```
    
    ```{js}
    $(document).ready(function() {
        document.getElementById("group").getElementsByClassName("selectized")[0].selectize.setValue("team 1", false);
    });
    
    ```
    
    Column {data-width=350}
    -----------------------------------------------------------------------
    
    ### Donuts Eaten
    
    ```{r}
    #Create function to make horizontal bar chart for donuts eaten
    plot_ly(data = my_tibble_shared, hoverinfo = "none") %>% 
        
    #adjust x-axis max to show data labels
    layout(
      xaxis = list(range = c(0, ~xaxis_max[1])),
      showlegend = F) %>% 
    
    #plot donuts eaten by donut type 
    add_trace(
      x = ~donuts_eaten,
      y = ~donut_type,
      type = "bar") %>%  
    
    #place text labels on chart 
    add_text(
      x = ~donuts_eaten,
      y = ~donut_type,
      text = ~donuts_eaten,
      textposition = "right",
      textfont = list(color = "black"),
      showlegend = F,
      texttemplate = paste('<b> %{text:,.1d} </b>')) 
    ```

也许存在更好的解决方案,但我确实找到了解决此问题的方法。

我绘制了我的 x-axis 最大变量图表,以便 x-axis 将其作为一个数据点。然后,我调整了我的 y-axis,以便包含我想要的 x-axis 最大值的条柱不会显示在图表上。

如果某人有 non-shiny flexdashboard,其中包含链接到串扰过滤器的绘图图形,这将很有帮助。此解决方案允许数据标签完全显示在所有过滤图表上,同时为每个过滤级别保持合适的 x-axis 范围。

我在上面的代码中添加了这个而没有改变任何其他内容:

#plot bar with xaxis max to adjust xaxis range 
add_trace(
  x = ~xaxis_max,
  y = "z_xaxis_max", 
  type = "bar",
  transforms = list(
    list(
      type = "aggregate",
      groups = ~group,
      aggregations = list(
        list(
          target = "x", func = "max", enabled = T))))) %>%   
  
#adjust x-axis max to show data labels
layout(
  showlegend = F, 
  yaxis = list(title = none, range = c(-0.5,3.5))) %>%