Search code examples
rbar-chartggvis

ggvis - How to show different variables in barchart, and select any or more dynamically


i have this data.frame:

    Campanas1     variable     sessions
1   Adwords       sumResults    69
2   Campa�as     sumResults     2
3   Directo       sumResults    10947
4   Email         sumResults    413
5   Referencias   sumResults    12991
6   SEO           sumResults    37693
7   Social Media  sumResults    5993
8   Others        sumResults    2

I've made a bar chart with this code:

Sesiones_Campanas1 %>% ggvis(~Campanas1, ~sessions, fill := "red") %>% layer_bars()

Question: How to put a selector for every/all Sources (from Campanas1)?

My attemp:

Sesiones_Campanas1 %>% ggvis(~Campanas1, ~sessions, fill := "red") %>%   layer_bars(input_select(label = "Fuente"),
                                                                              choices = c("Email", 
                                                                                "Directo", "Adwords",
                                                                                "Campanas1", "Referencias", "SEO",
                                                                                "Social Media"))

But i get this error:

Error: length(x) not equal to 1

This is what i want, but with interactivity


Solution

  • After quite a bit of a research I think I made it. The difficult part was to include the All-rows part in the drop-down menu. So, here it goes:

    First of all you need 2 libraries to do it: dplyr and stringi:

    library(dplyr)
    library(stringi)
    
    selector <- c('Adwords', 'Campanas', 'Directo', 'Email',  'Others', 'Referencias', 'SEO', 'Social_Media', 'All' = 'Adwords_Campanas_Directo_Email_Others_Referencias_SEO_Social_Media' ) 
    #the selector is a vector to include all your choices
    

    And the actual code that does what you need:

    Sesiones_Campanas1  %>%  #the table
      ggvis(~Campanas1, ~sessions, fill := "red") %>% #the ggvis object
      filter(stri_detect_fixed(eval(input_select(choices=selector, label='Fuente' )) , Campanas1) ) %>%   #the difficult part. You need to use filter and stri_detect_fixed with the input select to get exactly what you need.
      layer_bars() #plot bars
    

    I cannot upload the interactive graph (I think) so I ll upload the static all graph (But you can see the drop-down box and all of the choices there).

    P.S. If the x-axis labels won't appear correctly it is because you need to increase the graph size (works both on Rstudio and on the browser)

    P.S.2 A few words on how the filter line works: You need eval to evaluate the input_select outcome into a string so that it can then be matched with the stri_detect_fixed function. filter then decides which rows to be used.

    And that's it!

    enter image description here

    UPDATE

    In order to have the the 'all' sources selected at the beginning you need to specify the selected argument like this:

    Sesiones_Campanas1  %>% 
      ggvis(~Campanas1, ~sessions, fill := "red") %>%
      filter(stri_detect_fixed(eval(input_select(choices=selector, label='Fuente', selected='Adwords_Campanas_Directo_Email_Others_Referencias_SEO_Social_Media' )) , Campanas1) ) %>%   
      layer_bars() 
    

    Hope this helps!!