rshinyggvis

ggvis add_tooltip is not working with layer_bars


I have the following code that I am trying to build into the shiny dashboard. If I change to layer_point the code works.

I tried to change to layer_rect instead - however, I am unable to get a stacked bar chart with layer rects.

I have attached a photo below error.

enter image description here

color_choices <- c("#08519C","#238B45")

tab2_1 %>%  
  ungroup() %>%
  ggvis(x=~variable, y=~value, fill = ~Tender_won) %>%
  layer_bars() %>% 
  add_axis("y",title="Volume",title_offset=80) %>%
  add_axis("x",title ="Year") %>%
  scale_ordinal("fill",range = color_choices) %>%
  add_tooltip(function(data) paste0("test1",data$value,"test2")) %>%
  add_legend(scales = "fill", values = c("No","Yes"), title = "Tender Status")

The Data can be found here:

https://www.dropbox.com/s/3lq6qncru0xuvoc/helphelp.csv?dl=0


Solution

  • I'll use the hec example from the compute_stack documentation to show you some examples of how to add tooltips to a stacked bar plot. The exact nature of the tooltip will depend on which data you want the tooltip to display. My guess is you want to display the length of each stacked bar section (the actual y value from the original dataset).

    It helps me to know what the dataset looked like that layer_bars used for graphing. You can see this by using compute_stack directly and looking at the resulting dataset.

    hec = as.data.frame(xtabs(Freq ~ Hair + Eye, HairEyeColor))
    hec %>% compute_stack(~Freq, ~Hair)
    
    Source: local data frame [16 x 6]
    
         Hair    Eye  Freq group__ stack_upr_ stack_lwr_
       (fctr) (fctr) (dbl)  (fctr)      (dbl)      (dbl)
    1   Black  Brown    68   Black         68          0
    2   Brown  Brown   119   Brown        119          0
    3     Red  Brown    26     Red         26          0
    4   Blond  Brown     7   Blond          7          0
    5   Black   Blue    20   Black         88         68
    6   Brown   Blue    84   Brown        203        119
    7     Red   Blue    17     Red         43         26
    8   Blond   Blue    94   Blond        101          7
    9   Black  Hazel    15   Black        103         88
    10  Brown  Hazel    54   Brown        257        203
    11    Red  Hazel    14     Red         57         43
    12  Blond  Hazel    10   Blond        111        101
    13  Black  Green     5   Black        108        103
    14  Brown  Green    29   Brown        286        257
    15    Red  Green    14     Red         71         57
    16  Blond  Green    16   Blond        127        111
    

    Notice the upper and lower y coordinates used in plotting each section of each bar are called stack_upr_ and stack_lwr_. While these values are based on the original y variable Freq, the Freq values aren't directly plotted. I think this may be what tripped you up.

    Based on this info, if you want a tooltip to show the original y value you can simply subtract the lower y coordinate of each stack from the upper y coordinate within your tooltip.

    hec %>% ggvis(x = ~Hair, y = ~Freq, fill = ~Eye) %>%
        layer_bars() %>%
        add_tooltip(function(data) paste0("test1 ", data$stack_upr_ - data$stack_lwr_, " test2"))