Interactive bar chart with multiple conditioning variables and default shown distribution is unconditioned

56 views Asked by At

My aim is to make an interactive bar chart. The reader sees summary statistics of a variable, and can then choose to see the distribution of that variable conditioned on other variables. For example, they see the average total_bill for restaurant guests at lunch and dinner time. They can then choose to see how that varies by sex OR by age. Below is an example, which I modified from here.

library(ggplot)
library(plotly)

#Sex
dat1 <- data.frame(
  sex = c("Female", "Female", "Male", "Male", 'Any', 'Any'),
  time = c("Lunch", "Dinner", "Lunch", "Dinner", 'Lunch', 'Dinner'),
  total_bill = c(13.53, 16.81, 16.24, 17.42, 14.5, 17.3)
)

p <- ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
  geom_bar(stat="identity", position=position_dodge())

fig1 <- ggplotly(p)

fig1

#Age
dat2 <- data.frame(
  age = c('Old', 'Old', 'Young', 'Young', 'Any', 'Any'),
  time = c("Lunch","Dinner","Lunch","Dinner", 'Lunch', 'Dinner'),
  total_bill = c(14.53, 15.81, 18.24, 19.42, 14.5, 17.3)
)

p <- ggplot(data=dat2, aes(x=time, y=total_bill, fill=age)) +
  geom_bar(stat="identity", position=position_dodge())

fig2 <- ggplotly(p)

fig2

Here are the problems with this approach:

  1. in both plots, the total_bill for all three groups is shown per the default.
  2. conditioning on sex and age occurs in two separate plots. It would be nice with a dropdown menu to the left, where the user could choose to condition on sex/age.

I am open to other interactive solutions. The issue with shiny is that it requires me to host it on a server, and I need to be able to send the report to people so they can also have it when they are offline. There is this solution, but I am hoping to achieve this without having to learn a new programming language (i.e., OJS).

0

There are 0 answers