0

I am trying to create nested facets. In the example below, there are 4 experiments. Each experiment has 2 subjects with 2 test scores. I would still like each subj_num on a separate plot, but for each subj_num in a given exp_num to be linked, with a facet that extends across both plots. Is this possible?

library(tidyverse)

dat <- data.frame(exp_num = rep(1:4, each=4),
                  subj_num = rep(1:8, each=2),
                  test_num = rep(1:2, 2),
                  score = sample(16)
                  )

ggplot(dat, aes(test_num, score)) +
  geom_line() +
  facet_grid(exp_num+subj_num~.)

enter image description here

Adam_G
  • 7,337
  • 20
  • 86
  • 148
  • 1
    Did you see questions like these already? https://stackoverflow.com/questions/31690007/ggplot-drawing-line-between-points-across-facets What exactly did you try and where are you getting stuck? I'm not sure I understand what your desired output for this data is. – MrFlick Apr 25 '20 at 23:19
  • Is this what you are looking for? `dat <- dat %>% mutate(new = as.factor(paste(exp_num,subj_num))) ggplot(dat, aes(test_num, score)) + geom_line() + facet_grid(~new)` – Magnus Nordmo Apr 26 '20 at 13:24
  • Thanks for these. What I'm trying to show is that the two subjects in each experiment are linked. I know I can plot them on the same chart, but I need them on separate plots. Whether those two plots have a parent facet showing the exp_num, or those two are closer together. The key is to show that each subj is essentially in a group covered by exp_num. – Adam_G Apr 26 '20 at 17:40

0 Answers0