1
votes

Apologies for not using the correct "lingo", I don't know what it's called:

I have a table made up of data that represents participants' numeric responses to questions. The data table looks like the sample below (obviously shortened for clarity):

participant q1 q2 q3 q4 .... q10
     1       2  1 3   5 ....  2
     2       3  2 4   1 ....  4
     3       1  2 4   2 ....  3
     .
     .
     50      2  3 5   2 ....  5

So what I want to do is create a boxplot in ggplot that puts question number along the x axis, and score up the side. I know how to do a boxplot with just ONE question, but how can I do it for all ten?

If I do this:

susQBoxPlot <-ggplot(susQuestions, aes(x = participant, y = q1, group = 1)) 
+ geom_boxplot()
susQBoxPlot

Then I get this:

enter image description here

But where do I go from here? I thought I could just add the extra columns to the "y =" part of the aes, like this:

susQBoxPlot <-ggplot(susQuestions, aes(x = participant, y = q1, q2, group = 1)) 
+ geom_boxplot()

But it just gives me the same output.

Next I tried this:

susQBoxPlot <-ggplot(susQuestions, aes(x = participant, y = c(q1, q2), group = 1)) 
+ geom_boxplot()
susQBoxPlot

But I just get the following error:

Error: Aesthetics must be either length 1 or the same as the data (50): y

Whatever that means!

I've tried looking in ggplot documentation but I can't see anything that even remotely looks like what I'm trying to do.

And yes, I am aware that r has a built in boxplot() function, but I don't want to use that because I want my box plots and bar plots to have the same style, and I don't like the way the barplot() function in r works!

2

2 Answers

0
votes

You'll want to do something like

ggplot(tidyr::gather(susQuestions, q, val, -participant), aes(q, val, group=q)) + geom_boxplot()
0
votes

Ok, I got this working. Thanks go to Robin Gertenbach for his suggestion that I do the following:

ggplot(tidyr::gather(susQuestions, q, val, -participant), aes(q, val, group=q)) + geom_boxplot()

This created a box plot that gave me what I wanted, but the x-axis values were out of order (i.e. they went q1, q10, q2, q3, q4....). I found a solution to this here, and used Tjebo's solution.

In the end my code looked like this:

# Re-organise susQuestions data frame into long format:
questionData <- tidyr::gather(susQuestions, q, val, -participant)

# Create a vector of question levels
questionLevels <- c("q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10")

# Create box plot
susQBoxPlot <-ggplot(questionData, aes(x = factor(q, questionLevels), val, group=q)) + # Define data for plot
  stat_boxplot(geom ='errorbar', width=0.25) + # Add error bars
  geom_boxplot(fill = "red", colour = "black") + # Set fill colour to blue
  scale_y_continuous(name = "SUS Score", breaks = seq(1, 5, 1), limits=c(1, 5)) + # Set scale for y axis
  scale_x_discrete(name = "Question") + # Set x axis name
  ggtitle("Boxplot of SUS Question Responses") + # Set plot title
  theme_bw() + # Set black and white theme
  theme(plot.title = element_text(hjust = 0.5), # Centre plot title
        panel.grid.major = element_blank(), # Turn off major gridlines
        panel.grid.minor = element_blank()) # Turn off minor gridlines
susQBoxPlot # Display plot

Result:

enter image description here