6

I'm looking into building a shiny app with ggvis. For this I'm using a small dataset called "company". It contains employee data where each line represents and employee.

From a ggvis perspective I'm trying the following: Creating a bar chart that shows distribution for the following variables:

  • Age
  • Role
  • Sex

Instead of creating three different bar charts by using the following code:

#Barcharts - Role
company %>% ggvis(~Role,opacity := 0.8, fill:= "firebrick") %>%
  layer_bars() %>%
  scale_ordinal('x', domain=c('Analyst','Consultant','Software Engineer','Manager','Director'))

#Barcharts - Age
company %>% ggvis(~Age,opacity := 0.8, fill:= "firebrick") %>%
  layer_bars()

#Barcharts - Sex
company %>% ggvis(~Sex,opacity := 0.8, fill:= "firebrick") %>%    
  layer_bars()

I'd like to create a ggvis bar chart that allows an input selector.

I've tried the following code:

company %>% ggvis(input_select(c("Sex","Role","Age"), map = as.name)) %>% layer_bars()

The following error is returned:

Error: Visual property x.update is not a variable

The data used would be:

raw <- "Age Sex Role
35   M          Director
37   M          Director
30   M           Manager
28   M           Manager
28   F           Manager
27   M Software_Engineer
25   M        Consultant
26   M        Consultant
25   F           Analyst
25   M           Analyst
25   M           Analyst
25   M           Analyst
25   M           Analyst
25   M           Analyst
25   F           Analyst
25   F           Analyst
25   F           Analyst
24   F           Analyst
25   M           Analyst"


company = read.table(textConnection(raw), header=TRUE)

This makes me believe that ggvis does not allow the x variable to be an input selector item. Is this correct? Is there a solution for this?

Thank you in advance. Kind regards

lnNoam
  • 1,055
  • 11
  • 20
glnvdl
  • 397
  • 2
  • 12

1 Answers1

8

First of all you need to melt your data.frame like this:

library(reshape2)
company <- melt(company, measure.vars=c('Role','Age','Sex'))

And then the following worked for me:

#Barcharts - Age
company %>% ggvis(~value, opacity := 0.8) %>%
  #use filter to pick only the category you want
  filter(variable == eval(input_select(choices=c('Role','Age','Sex')))) %>%
  layer_bars()

I cannot upload the interactive version but the plot looks like this:

enter image description here

And you can pick the category you like

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • First of all, thank you for this solution! I have two questions regarding your answer: * Why do you melt the dataframe? * Is there now a way to order the x-axis back to 'Analyst','Consultant','Software Engineer','Manager','Director' rather than the alphabetical order? Thank you in advance. – glnvdl Jun 28 '15 at 17:54
  • No problem :), glad I could be of help. We melt the data.frame so that we have all of our data in long format i.e. have all the categories in rows. This way we can then use filter and essentially pick only the category we want each time (from the drop down box). Unfortunately, we cannot use `scale_ordinal('x', domain=c('Analyst','Consultant','Software_Engineer','Manager','Director'))` in this case because if you swap to sex or age the graphs will look bad. The only solution i can think of for this is to use Shiny with ggvis. – LyzandeR Jun 28 '15 at 18:04
  • In more detail: `filter` will only pick the rows where the column variable is the value of the dropdown box. And we use melt in order to create the variable column in the format needed. – LyzandeR Jun 28 '15 at 18:06
  • Thank you for the explanation. This essentially means that, in order to get the full functionality, it's better to develop the complete solution in Shiny directly? – glnvdl Jun 28 '15 at 18:08
  • I find that Shiny gives you the freedom to do anything you like using any library you want. Combining this with ggvis 's additional interactivity you can do amazing things. `ggvis` is really good on its own, but if you want to implement data manipulation (ordering a data.frame for example) Shiny would probably make your life easier. – LyzandeR Jun 28 '15 at 18:11
  • Thank you for the advice. My idea was to build my first shiny app upon the ggvis manipulations I'm doing now. Looks like I might want to rethink building everything in ggvis first. A better idea would be to start building in Shiny and work my way through the packages I need combining it with additional flexibility. Thanks for the advice! – glnvdl Jun 28 '15 at 18:19