Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Facet wrap with more than one variable

I am trying to facet wrap with two variables, but once I add the variable "Group" I get this error: Error in sanitise_dim(nrow) : object 'Group' not found

Here is my dataframe:

dput(df)
structure(list(Condition = c("A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B"), Gate = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 2L, 2L, 
3L, 3L, 4L, 4L, 5L, 5L), .Label = c("Gate 0", "Gate 1", "Gate 2", 
"Gate 3", "Full"), class = "factor"), Modality = c("AO", "AO", 
"AO", "AV", "AV", "AV", "AO", "AO", "AO", "AV", "AV", "AV", "AO", 
"AO", "AO", "AV", "AV", "AV", "AO", "AO", "AO", "AV", "AV", "AV", 
"AO", "AO", "AO", "AV", "AV", "AV", "AO", "AV", "AO", "AV", "AO", 
"AV", "AO", "AV", "AO", "AV"), Participant = c(1, 2, 4, 1, 2, 
4, 1, 2, 4, 1, 2, 4, 1, 2, 4, 1, 2, 4, 1, 2, 4, 1, 2, 4, 1, 2, 
4, 1, 2, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), Group = c("Group 2", 
"Group 1", "Group 2", "Group 2", "Group 1", "Group 2", "Group 2", 
"Group 1", "Group 2", "Group 2", "Group 1", "Group 2", "Group 2", 
"Group 1", "Group 2", "Group 2", "Group 1", "Group 2", "Group 2", 
"Group 1", "Group 2", "Group 2", "Group 1", "Group 2", "Group 2", 
"Group 1", "Group 2", "Group 2", "Group 1", "Group 2", "Group 1", 
"Group 1", "Group 1", "Group 1", "Group 1", "Group 1", "Group 1", 
"Group 1", "Group 1", "Group 1"), Accuracy = c(0, 0.5, 0, 0, 
0, 0.3, 0, 0.5, 0.7, 0.1, 0.5, 0.7, 0.2, 0.8, 0.7, 0.2, 0.8, 
0.9, 0.2, 0.9, 0.9, 0.4, 0.9, 1, 0.4, 1, 0.9, 0.3, 1, 1, 0, 0, 
0.2, 0.2, 0.2, 0.4, 0.6, 0.7, 0.6, 0.7), se = c(0, 0.166666666666667, 
0, 0, 0, 0.152752523165195, 0, 0.166666666666667, 0.152752523165195, 
0.1, 0.166666666666667, 0.152752523165195, 0.133333333333333, 
0.133333333333333, 0.152752523165195, 0.133333333333333, 0.133333333333333, 
0.1, 0.133333333333333, 0.1, 0.1, 0.163299316185545, 0.1, 0, 
0.163299316185545, 0, 0.1, 0.152752523165195, 0, 0, 0, 0, 0.133333333333333, 
0.133333333333333, 0.133333333333333, 0.163299316185545, 0.163299316185545, 
0.152752523165195, 0.163299316185545, 0.152752523165195)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -40L), groups = structure(list(
    Condition = c("A", "A", "A", "A", "A", "A", "A", "A", "A", 
    "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
    "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", 
    "B", "B", "B", "B", "B", "B", "B"), Gate = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 
    1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L), .Label = c("Gate 0", 
    "Gate 1", "Gate 2", "Gate 3", "Full"), class = "factor"), 
    Modality = c("AO", "AO", "AO", "AV", "AV", "AV", "AO", "AO", 
    "AO", "AV", "AV", "AV", "AO", "AO", "AO", "AV", "AV", "AV", 
    "AO", "AO", "AO", "AV", "AV", "AV", "AO", "AO", "AO", "AV", 
    "AV", "AV", "AO", "AV", "AO", "AV", "AO", "AV", "AO", "AV", 
    "AO", "AV"), Participant = c(1, 2, 4, 1, 2, 4, 1, 2, 4, 1, 
    2, 4, 1, 2, 4, 1, 2, 4, 1, 2, 4, 1, 2, 4, 1, 2, 4, 1, 2, 
    4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), .rows = structure(list(
        1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
        14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
        25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
        36L, 37L, 38L, 39L, 40L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -40L), .drop = TRUE))

And here is the code I am running:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df%>%
  group_by(Condition, Gate = fct_inorder(Gate), Modality, Group) %>% 
  dplyr::summarize(Proportion_Correct = mean(Accuracy), #item-level: Accuracy = mean(Correct) --> participant-level: Proportion_Correct = mean(Accuracy)
                   standarderror = (sd(Accuracy, na.rm = TRUE)/sqrt(n()))) %>% 
  ggplot(aes(x = Gate, y = Proportion_Correct, color = Modality, group = Modality)) + 
  geom_line() + 
  geom_errorbar(aes(ymin = Proportion_Correct - standarderror, ymax = Proportion_Correct + standarderror), color = "Black", size = .15, width = .3) + 
  geom_point(size = 2)+ 
  scale_y_continuous(labels = scales::percent)+ 
  labs(y= "Accuracy", x = "Gate")+
  facet_wrap(~Condition, Group) + 
  theme_minimal() + 
  scale_color_brewer(palette = "Set1")+
  ggtitle("Performance")

When I run this code, I get that error. I am hoping to get three graphs side by side. There should be two conditions for Group 1, and only one condition for Group 2. Any ideas are appreciated. Thank you!!

>Solution :

I believe that in the facet wrap condition, when you use multiple variable names you separate them with "+" sign and not a comma. So, your code should be:

df%>%
  group_by(Condition, Gate = fct_inorder(Gate), Modality, Group) %>% 
  dplyr::summarize(Proportion_Correct = mean(Accuracy), #item-level: Accuracy = mean(Correct) --> participant-level: Proportion_Correct = mean(Accuracy)
                   standarderror = (sd(Accuracy, na.rm = TRUE)/sqrt(n()))) %>% 
  ggplot(aes(x = Gate, y = Proportion_Correct, color = Modality, group = Modality)) + 
  geom_line() + 
  geom_errorbar(aes(ymin = Proportion_Correct - standarderror, ymax = Proportion_Correct + standarderror), color = "Black", size = .15, width = .3) + 
  geom_point(size = 2)+ 
  scale_y_continuous(labels = scales::percent)+ 
  labs(y= "Accuracy", x = "Gate")+
  facet_wrap(~Condition+Group) + 
  theme_minimal() + 
  scale_color_brewer(palette = "Set1")+
  ggtitle("Performance")

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading