Justus Blaschke
Justus Blaschke

Reputation: 27

R - Plot correctness of answers of every single question of a questionnaire using multiple factor levels

I got data from a questionnaire of students of different school types. The questionnaire contains 14 questions (0=false, 1=right) and there are 4 different school types. I am now trying to plot the average correctness of every question by school type. I thought that I could plot the questions continous on the x-axis maybe as bars.

The data looks like:

Nr School type Quest1 Quest2 Quest3 Quest4 Quest5
1 Primary 1 0 0 1 0
2 Secondary 0 0 1 1 0
3 Higher 1 1 1 0 1
4 Secondary 0 0 1 1 1
5 Primary 0 0 0 0 0
6 Primary 1 1 0 0 0
7 Higher 1 1 1 1 1
8 Secondary 0 0 1 1 1

Has anyone an idea?

Thanks a lot!

Upvotes: 1

Views: 36

Answers (1)

Duck
Duck

Reputation: 39613

Maybe this approach can be useful:

library(tidyverse)
#Code
df %>% pivot_longer(-School.type) %>%
  group_by(School.type,name) %>%
  summarise(Avg=mean(value==1)) %>%
  ggplot(aes(x=name,y=Avg,fill=School.type))+
  geom_bar(stat = 'identity',position = position_dodge(0.9),color='black')+
  theme_bw()+xlab('Question')

Output:

enter image description here

Some data used:

#Data
df <- structure(list(School.type = c("Primary", "Secondary", "Higher", 
"Secondary", "Primary", "Primary", "Higher", "Secondary"), Quest1 = c(1L, 
0L, 1L, 0L, 0L, 1L, 1L, 0L), Quest2 = c(0L, 0L, 1L, 0L, 0L, 1L, 
1L, 0L), Quest3 = c(0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L), Quest4 = c(1L, 
1L, 0L, 1L, 0L, 0L, 1L, 1L), Quest5 = c(0L, 0L, 1L, 1L, 0L, 0L, 
1L, 1L)), class = "data.frame", row.names = c(NA, -8L))

Upvotes: 1

Related Questions