rggplot2bar-chartpercentagemultiple-variable-return

How to plot Bar_Chart of accuracy scores as percentage value for multiple variables and subgroups using ggplot2 R?


I thank you for considering helping me with this.

I have raw data points for Accuracy (0 and 1) and Reaction time (in msec) for key press response for 7 blocks of repeated measures. The data is arranged in a long format (for running GLMM purposes), and there are multiple subgroups: 1) There are two groups: DD and TD, and 2) within each group there are two subgroups: AgeRange-I and AgeRange-II, and all the DDs and TDs performed two types of task conditions: SOC and MOC.

I would like to plot Accuracy scores in percentages (on the y-axis) for 7 blocks (on the x-axis) for two groups, DD and TD, in different colour fills. Because of subgroups,I'm thinking of plotting four graphs: 1) SOC in AgeRange-I 2) MOC in AgeRange-I 3) SOC in AgeRange-II and MOC in AgeRange-II so that the difference for DD and TD across the block for each task condition in the two Age Ranges can be visualised.

For example, below is my sample data frame: each participant has 420 responses for each sequence type (SOC and MOC) with 60 responses per block.

    X.1     X image corrAns KeyPosition   Acc    RT thisN thisTrialN SeqType Group ParticipantID   Age Gender date       Block AgeRange AccSum AccPercent
   <int> <int> <chr> <chr>   <chr>       <int> <dbl> <int>      <int> <chr>   <chr> <chr>         <int> <chr>  <chr>      <fct> <chr>     <int>      <dbl>
 1     1     1 1.png up      up              1 0.725     0          0 MOC     TD    ADVIK            12 M      2022_Jul_… rand1 II           57         95
 2     2     2 3.png down    down            1 0.461     1          1 MOC     TD    ADVIK            12 M      2022_Jul_… rand1 II           57         95
 3     3     3 4.png left    left            1 0.495     2          2 MOC     TD    ADVIK            12 M      2022_Jul_… rand1 II           57         95
 4     4     4 1.png up      up              1 0.695     3          3 MOC     TD    ADVIK            12 M      2022_Jul_… rand1 II           57         95
 5     5     5 2.png right   right           1 0.672     4          4 MOC     TD    ADVIK            12 M      2022_Jul_… rand1 II           57         95
 6     6     6 4.png left    left            1 0.600     5          5 MOC     TD    ADVIK            12 M      2022_Jul_… rand1 II           57         95
 7     7     7 3.png down    down            1 0.458     6          6 MOC     TD    ADVIK            12 M      2022_Jul_… rand1 II           57         95
 8     8     8 4.png left    left            1 0.654     7          7 MOC     TD    ADVIK            12 M      2022_Jul_… rand1 II           57         95
 9     9     9 1.png up      up              1 0.520     8          8 MOC     TD    ADVIK            12 M      2022_Jul_… rand1 II           57         95
10    10    10 3.png down    down            1 0.460     9          9 MOC     TD    ADVIK            12 M      2022_Jul_… rand1 II           57         95
# ℹ 40,310 more rows

Basically, this is a graph for SOC-AgeRange-1; this is the kind of graph I aim to achieve in r (ggplot2).

enter image description here


Solution

  • You haven't given us enough of your data to produce a plot with the desired structure, but your description is adequate to allow a reproducible example with the same structure (see below). Essentially it sounds as though you want columns showing the mean of Accuracy (y axis), with the Block on the x axis, and the columns to be filled and dodged according to Group, then faceted by both Age Group and Sequence Type. The code for this would be something like:

    library(ggplot2)
    
    ggplot(data, aes(Block, Acc, fill = Group)) +
      stat_summary(fun = mean, geom = "col", position = "dodge") +
      facet_wrap(. ~ paste("Age Range:", AgeRange) + 
                     paste("Sequence Type:", SeqType)) +
      scale_fill_brewer(palette = "Set1") +
      theme_minimal(base_size = 16)
    

    enter image description here

    Created on 2023-08-12 with reprex v2.0.2