How to: Create a plot for 3 categorical variables and a continuous variable in R?

Viewed 741

I would like to create a plot using R, preferably by using ggplot. I have the following variables to visualize, most of them binary:

Trial: cong/incong

Sentence: him/himself

Condition: normal/slow

Accuracy: number

SE: number

structure(list(TrialType = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L), .Label = c("congruent", "incongruent"), class = "factor"), 
    SentenceType = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L
    ), .Label = c("him", "himself"), class = "factor"), Condition = structure(c(1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("normal_speech", 
    "slow_speech"), class = "factor"), MeanAccuracy = c(0.794871794871795, 
    0.762820512820513, 0.967948717948718, 0.967948717948718, 
    0.237179487179487, 0.342105263157895, 0.942307692307692, 
    0.83974358974359), SE = c(0.0342056016493384, 0.0430264468743046, 
    0.0389087806837746, 0.0496183045476835, 0.0135583881898854, 
    0.0163760608630386, 0.0170869868584354, 0.0311270245470197
    )), class = "data.frame", row.names = c(NA, -8L))

The SE stands for the standard error, meaning that I would like to present the error bars around the accuracy score.

I figured that my best option is to make two bar plots, One separately for each condition with accuracy on the x-axis. Then, four bars representing both possible combinations of sentence and trial, showing the accuracy in height and error bars presented around this to reflect the uncertainty.

How could I make such a graph? Or, does anyone think that this is not the right type of graph and then what would be (and how to plot it...)?

Thanks in advance!

2 Answers

Are you perhaps looking for something like this?

library(ggplot2)

ggplot(df, aes(TrialType, MeanAccuracy, fill = SentenceType)) +
  geom_col(position = position_dodge(width = 1), color = "gray50") +
  geom_errorbar(aes(ymin = MeanAccuracy - SE, 
                    ymax = MeanAccuracy + SE), width = 0.25,
                position = position_dodge(width = 1)) +
  scale_fill_manual(values = c("gold", "deepskyblue4")) +
  facet_grid(.~Condition, switch = "x") +
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_blank(),
        panel.border = element_blank(),
        panel.spacing = unit(0, "points"),
        axis.line = element_line())

enter image description here

Using some simulated data based on the description you shared, you can try:

library(ggplot2)
library(dplyr)
library(tidyr)
#Data
df <- data.frame(Trial=rep(c('cong','incong'),4),
                 Sentence= rep(c('him','himself'),4),
                 Condition=rep(c('normal','slow'),4),
                 Accuracy=runif(8,0,1),
                 SE=runif(8,0,10),stringsAsFactors = F)
#Plot 1
df %>% pivot_longer(-c(Trial,Sentence,Condition)) %>%
  ggplot(aes(x=name,y=value,fill=Condition))+
  geom_bar(stat = 'identity')+
  facet_wrap(.~Trial+Sentence,scales = 'free')

Output:

enter image description here

Or this:

#Plot 2
df %>% pivot_longer(-c(Trial,Sentence,Condition)) %>%
  ggplot(aes(x=name,y=value,fill=Condition))+
  geom_bar(stat = 'identity')+
  facet_grid(Trial~Sentence,scales = 'free')

Output:

enter image description here

Further details and data are necessary to understand your issue.

Related