How to create small multiples chart in R?

52 Views Asked by At

I'm new to R and trying to do some exploratory analysis. I have a dataset with multiple columns, 3 of them look like this

Q1 Q2 Q3
1  6  2
2  6  7
2  6  6
6  6  6
7  1  1
6  6  1

where Qs are yes/no questions and 1,2,6,7 are encoded for "Yes", "No", "Don't know", and "Valid skip"

I want to create small multiples plot from these 3 columns that shows the frequency of each type of answer grouped by question. The approach I'm trying to do is to transform these columns to a new data frame with 3 variables: Question (that takes 3 values Q1, Q2, Q3), Answer (takes 4 values 1, 2, 6, 7), and Frequency, then I can create small multiples.

Could you show me how to do this transformation? And if you know other ways to create the chart, please share as I would love to know different ways to do it.

Thanks a lot!

1

There are 1 best solutions below

0
jpsmith On

You could try using the dplyr and tidyr libraries:

library(tidyr)
library(dplyr)

df %>% 
  pivot_longer(Q1:Q3, names_to = "question", values_to = "answer") %>% 
  group_by(question) %>% 
  count(answer) %>%
  rename(frequency = n)

Output:

   question answer frequency
   <chr>     <int>     <int>
 1 Q1            1         1
 2 Q1            2         2
 3 Q1            6         2
 4 Q1            7         1
 5 Q2            1         1
 6 Q2            6         5
 7 Q3            1         2
 8 Q3            2         1
 9 Q3            6         2
10 Q3            7         1

Data

df <- read.table(text = "Q1 Q2 Q3
1  6  2
2  6  7
2  6  6
6  6  6
7  1  1
6  6  1", header = TRUE)