Can you create a graph with three y values and three different grouping variables at once?

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...

Graph that I'm after

... 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.

Create your normal graph first with ggplot(...) + ...
At the end, add facet_wrap(~sex) for starters
Try this

ggplot(...) +
  geom_bar(...) + # one graph should be generated up to this point without '+'
  facet_wrap(~sex)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.