Hi all!
I'm currently working on creating a graph that would display 3 means for each of the 2x2x2 factorial design groups.
Here's my reprex:
####Reproducible Example
set.seed(44)
n <- 48
Condition <- c("Exp", "Control")
Sex <- c("Male", "Female")
Ideology <- c("Conservative", "Ideology")
dat <- data.frame(id = 1:n,
tidyr::crossing(Condition, Sex, Ideology),
Shoe_Size = sample(1:7, n, replace = TRUE),
Hat_Size = sample(1:7, n, replace = TRUE),
Glove_Size = sample(1:7, n, replace = TRUE))
> head(dat)
id Condition Sex Ideology Shoe_Size Hat_Size Glove_Size
1 1 Control Female Conservative 1 1 6
2 2 Control Female Ideology 3 5 3
3 3 Control Male Conservative 3 5 4
4 4 Control Male Ideology 1 2 1
5 5 Exp Female Conservative 6 2 6
6 6 Exp Female Ideology 4 3 7
My goal is to create a graph like this...
... without doing it manually like this:
####Graph Example
library(ggplot2)
dat.graph <- data.frame(Condition = c("Exp", "Exp", "Exp", "Exp", "Control", "Control", "Control", "Control",
"Exp", "Exp", "Exp", "Exp", "Control", "Control", "Control", "Control",
"Exp", "Exp", "Exp", "Exp", "Control", "Control", "Control", "Control"),
Sex = c("Male", "Male", "Female", "Female", "Male", "Male", "Female", "Female",
"Male", "Male", "Female", "Female", "Male", "Male", "Female", "Female",
"Male", "Male", "Female", "Female", "Male", "Male", "Female", "Female"),
Ideology = c("Conservative", "Liberal","Conservative", "Liberal","Conservative", "Liberal","Conservative", "Liberal",
"Conservative", "Liberal","Conservative", "Liberal","Conservative", "Liberal","Conservative", "Liberal",
"Conservative", "Liberal","Conservative", "Liberal","Conservative", "Liberal","Conservative", "Liberal"),
Clothes = c("Shoes","Shoes","Shoes","Shoes","Shoes","Shoes","Shoes","Shoes",
"Hats","Hats","Hats","Hats","Hats","Hats","Hats","Hats",
"Gloves","Gloves","Gloves","Gloves","Gloves","Gloves","Gloves","Gloves"),
Mean_Size = c(3.16, 2.5, 2.1, 7, 5.1, 2.9, 2.1, 6.4,
2.63, 3.1, 3.61, 4.4, 3.7, 2.1, 1.2, 2.7,
5.7, 3.2, 2.1, 2.6, 3.1, 6.2, 2.1, 2.6))
ggplot(data = dat.graph, aes(x = Clothes, y = Mean_Size, fill = Ideology)) +
geom_bar(stat = "identity", width = .5, position = "dodge") +
facet_wrap(~ Condition + Sex, ncol = 6, drop = FALSE) +
theme(
axis.text.x = element_text(angle=50, hjust=1)
)
Note: For the sake of time, the Mean_Size values are not the actual means from the dat
data frame. To get these I'd have to use the aggregate()
function for each of the numeric variables.
Is it possible to create a graph like the one presented above without doing it manually?
I'm trying my best to explain my question as clearly as I can at this stage of my learning so my apologies if anything is not 100% clear yet.
Any suggestions or tips will be of massive help!
Many thanks,
J.