0

I would like some help to code a nested figure using gglot2. The figure is about participants' payoff over periods. There are 60 periods which were partitioned into 12 independent trials. The main reason is that in each trial the maximum payoff one can reach stays the same. An example I find from previous study is attached.

Different from the example, there are 6 interaction conditions in my study: 2 (transparency vs no transparency) x3(no flexibility nor repair, flexibility and repair), so there should be 6 geom_lines in each trial from trial 1 to trial 12. The table head looks like this:

  trial period transparency    flexibility    repair    payoff  
  <int>  <int> <chr>           <chr>          <chr>      <dbl>    
1     1      1 no transparency no flexibility no repair   21.2      
2     1      1 no transparency flexbility     no repair   23.2      
3     1      1 no transparency flexbility     repair      19.3     
4     1      1 transparency    no flexibility no repair   15       
5     1      1 transparency    flexbility     no repair   20.3      
6     1      1 transparency    flexbility     repair      18.8    

the x_aes should be the 12 trials, then within each trial, there is a second layer (5 periods)

the y_aes should be the payoff per period.

nested plots enter image description here

Tung
  • 26,371
  • 7
  • 91
  • 115
Ja Mah
  • 27
  • 4
  • 1
    Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Dec 15 '19 at 06:42
  • thanks for the welcome, I am new to R and I will have a look these links you shared. – Ja Mah Dec 15 '19 at 06:51
  • Also, please explain how provided data relates to the image you posted – pogibas Dec 15 '19 at 07:54

1 Answers1

1

One possibility is to do the following. Since the data you provided, doesn't vary over trial or period, you'll mock up some data with similar structure:

df <- expand.grid(
  trial = 1:12,
  period = 1:5,
  transparency = c("no transparency", "transparency"),
  flexibility = c("no flexibility", "flexibility"),
  repair = c("no repair", "repair")
)
df$payoff <- rnorm(nrow(df))

We can then just facet over the trial, use the period as x-axis and payout as y-axis. How you choose to encode the transparency, repair and flexibility is up to you.

ggplot(df, aes(period, payoff)) +
  geom_line(aes(colour = interaction(flexibility, repair), linetype = transparency)) +
  geom_point(aes(colour = interaction(flexibility, repair), shape = transparency)) +
  facet_grid(~ trial)

enter image description here

teunbrand
  • 33,645
  • 4
  • 37
  • 63