Barplot of dummy categorical variables

Viewed 77

My data looks like this:

library(tidyverse)
#> Warning: package 'readr' was built under R version 4.1.2

set.seed(123)

dat <- tibble(
  mcq_option_1 = sample(c(0,1), replace = TRUE, size = 100),
  mcq_option_2 = sample(c(0,1), replace = TRUE, size = 100),
  mcq_option_3 = sample(c(0,1), replace = TRUE, size = 100),
  mcq_option_4 = sample(c(0,1), replace = TRUE, size = 100),
  mcq_option_5 = sample(c(0,1), replace = TRUE, size = 100)
)

Created on 2022-02-21 by the reprex package (v2.0.1)

My goal is to create a barplot of the times each mcq option was selected (counts of 1s in each column). What is the quickest data manipulation technique to achieve this? Should I pivot longer first and then create a new count variable using mutate? Or can I quickly do this inside of the ggplot function?

Is there a shorter/better solution than this?

new <- dat %>% pivot_longer(
  cols = everything(),
  names_to = 'options',
  values_to = 'active'
)

new %>% filter(active == 1) %>% group_by(options) %>% summarise(n = n())
2 Answers

Not sure if you're looking for better execution speed or simply shorter code. You can at least get the code a bit shorter by first summing across columns, then pivoting.

dat %>% 
  summarise(across(.fns = sum)) %>% 
  pivot_longer(everything()) %>% 
  ggplot(aes(name, value)) +
  geom_col()

If you just need to know the number of 1s in a vector of 1 and 0, you can simply take the sum. This can be done inside ggplot::stat_summary() by specifying fun = "sum". This avoids some of the extra steps you introduce with filter(), group_by() & summarize().

library(tidyverse)

set.seed(123)
dat <- tibble(mcq_option_1 = sample(c(0,1), replace = TRUE, size = 100),  mcq_option_2 = sample(c(0,1), replace = TRUE, size = 100),  mcq_option_3 = sample(c(0,1), replace = TRUE, size = 100),  mcq_option_4 = sample(c(0,1), replace = TRUE, size = 100),  mcq_option_5 = sample(c(0,1), replace = TRUE, size = 100))

dat %>%
  pivot_longer(cols = everything(),
               names_to = 'options',
               values_to = 'active') %>%
  ggplot(aes(options, active)) +
  stat_summary(fun = "sum", geom = "bar")

Created on 2022-02-21 by the reprex package (v2.0.1)

Related