#--------------------------
Answer after question edits.
Error bar alignment is done by the call to position_dodge
. Invalid/valid reordering is a call to fct_rev
. Statistical comparison depends on what you actually want to show, and then try to figure out how you want to show it.
library(tidyverse)
b <- tribble(
~participant, ~cue, ~distance, ~RT, ~time, ~group,
"P1", "valid", "far", 1461, "T1", 4,
"P1", "invalid", "near", 1416, "T1", 4,
"P1", "invalid", "near", 1409, "T1", 4,
"P1", "invalid", "far", 1351, "T1", 4,
"P1", "invalid", "far", 1391, "T1", 4,
"P1", "invalid", "far", 1365, "T1", 4,
"P1", "invalid", "far", 1385, "T1", 4,
"P1", "invalid", "near", 1465, "T1", 4,
"P1", "valid", "near", 1451, "T1", 4,
"P1", "valid", "near", 1397, "T1", 4,
"P1", "valid", "far", 1466, "T1", 4,
"P1", "invalid", "far", 1411, "T1", 4,
"P1", "invalid", "near", 1439, "T1", 4,
"P1", "valid", "far", 1328, "T1", 4,
"P1", "valid", "far", 1437, "T1", 4,
"P1", "valid", "far", 1376, "T1", 4,
"P1", "invalid", "far", 1364, "T1", 4,
"P1", "invalid", "near", 1451, "T1", 4,
"P1", "valid", "far", 1461, "T1", 4,
"P1", "invalid", "far", 1441, "T1", 4,
"P1", "valid", "near", 1491, "T1", 4,
"P1", "valid", "near", 1385, "T1", 4,
"P1", "valid", "near", 1553, "T1", 4,
"P1", "invalid", "far", 1484, "T1", 4,
"P1", "valid", "far", 1449, "T1", 4,
"P1", "invalid", "near", 1361, "T1", 4,
"P1", "invalid", "near", 1399, "T1", 4,
"P1", "invalid", "near", 1389, "T1", 4,
"P1", "valid", "near", 1378, "T1", 4,
"P1", "valid", "near", 1365, "T1", 4,
"P1", "valid", "far", 1465, "T1", 4,
"P1", "valid", "near", 1333, "T1", 4,
"P1", "valid", "near", 1340, "T1", 4,
"P1", "invalid", "far", 1347, "T1", 4,
"P1", "valid", "far", 1375, "T1", 4,
"P1", "valid", "near", 390, "T2", 4,
"P1", "invalid", "far", 394, "T2", 4,
"P1", "invalid", "near", 374, "T2", 4,
"P1", "valid", "far", 363, "T2", 4,
"P1", "valid", "near", 342, "T2", 4,
"P1", "invalid", "far", 421, "T2", 4,
"P1", "invalid", "near", 398, "T2", 4,
"P1", "invalid", "near", 419, "T2", 4
)
b %>%
group_by(participant, cue, distance, time, group) %>%
summarise(RT_mean = mean(RT),
RT_sd = sd(RT)) %>%
filter(participant == "P1") %>% #not strictly necessary in this instance, but will be in general.
mutate(cue = fct_rev(cue)) %>%
ggplot(aes(x=cue, y=RT_mean, fill = distance))+
geom_bar(stat="identity", position = position_dodge(), width = .9)+
facet_grid(group~time, space="free_x") +
geom_errorbar(aes(ymin= RT_mean - RT_sd, ymax = RT_mean + RT_sd),
width = 0.2, color = "BLACK",
position=position_dodge(0.9))+ #the 0.9 here should the same value as the width in geom_bar
# to keep the error bar centred.
coord_cartesian(ylim = c(200,1500))+theme(legend.title = element_blank())
#> `summarise()` regrouping output by 'participant', 'cue', 'distance', 'time' (override with `.groups` argument)
Created on 2021-01-27 by the reprex package (v0.3.0)
#--------------------------
Original answer
I've made a dummy dataset, and think you may have a data quality issue. See that the first two rows of b
have the same cue, time, and group, but different RT. Can you post your original data?
The ordering of "valid"/"invalid" can be reversed using the forcats package, as in my second example.
library(tidyverse)
cue <- c("invalid", "invalid","valid","invalid","valid","invalid","valid","invalid","valid")
time <- c("T1", "T1","T1","T2","T2","T1","T1","T2","T2")
group <- c(1, 1,1,1,1,2,2,2,2)
RT <- c(1000, 1200,1300,400,500,700,800,300,400)
ci <- c(50, 100,100,100,100,50,50,50,50)
b <- tibble(cue,time,group,RT,ci)
b
#> # A tibble: 9 x 5
#> cue time group RT ci
#> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 invalid T1 1 1000 50
#> 2 invalid T1 1 1200 100
#> 3 valid T1 1 1300 100
#> 4 invalid T2 1 400 100
#> 5 valid T2 1 500 100
#> 6 invalid T1 2 700 50
#> 7 valid T1 2 800 50
#> 8 invalid T2 2 300 50
#> 9 valid T2 2 400 50
ggplot(b,aes(x=cue, y=RT, fill = cue))+
geom_bar(stat="identity", position = position_dodge(), width = .9)+
facet_grid(group~time, space="free_x") +
geom_errorbar(aes(ymin= RT - ci, ymax = RT + ci), width = 0.2, color = "BLACK", position=position_dodge())+
coord_cartesian(ylim = c(200,1500))+theme(legend.title = element_blank())
#reverse the order of the "invalid"/"valid"
b %>%
mutate(cue = fct_rev(cue)) %>%
ggplot(aes(x=cue, y=RT, fill = cue))+
geom_bar(stat="identity", position = position_dodge(), width = .9)+
facet_grid(group~time, space="free_x") +
geom_errorbar(aes(ymin= RT - ci, ymax = RT + ci), width = 0.2, color = "BLACK", position=position_dodge())+
coord_cartesian(ylim = c(200,1500))+theme(legend.title = element_blank())
Created on 2021-01-27 by the reprex package (v0.3.0)