Load packages and read data first.

#load packages
library(readspss) #package to read the original datafile from OFS
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.4     ✓ purrr   0.3.4
## ✓ tibble  3.1.2     ✓ dplyr   1.0.6
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
#read data
data <- read.sav("Humiston & Wamsley 2019 data.sav")

#remove excluded 
cleandata <- data %>%     #remove excluded participants 
  filter(exclude=="no")

Figuring out what to put into the figure

First, we’ll have a look at the graph we want to replicate. This is a screenshot from the original report:

We need to figure out how to calculate the differential bias change (for the y axis). However, luckily for us we don’t need to calculate the x axis since that is already done.

Calculating y-axis: differential bias change

According to the paper, differential bias change is calculated by “baseline minus delayed score for uncued bias subtracted from the baseline minus delayed score for cued bias”.

So the equation would look something like this:

(baseline_cued - delayed_cued) - (baseline_uncued - delayed_uncued)

We would then have to apply this to each participant’s scores.

Firstly, we have to mutate a bunch of extra columns to get what we want for the y-axis. So we mutate to get one set of brackets, again for the other set, and once again to get the final differential bias change.

differential <- cleandata %>%
  select(ParticipantID, baseIATcued, weekIATcued, baseIATuncued, weekIATuncued, SWSxREM) %>%
  mutate(cued_differential = baseIATcued - weekIATcued,
         uncued_differential = baseIATuncued - weekIATuncued,
         diff_bias_change = cued_differential - uncued_differential) #this is the y-axis

print(differential)
##    ParticipantID baseIATcued weekIATcued baseIATuncued weekIATuncued SWSxREM
## 1            ub6  0.57544182  0.20377367    0.60953653    0.68277422     276
## 2            ub7  0.09911241  0.45873715    0.64396538   -0.01070460       0
## 3            ub8  0.20577365  0.39859469    1.52435622    0.71187286     408
## 4            ub9  0.35314196  0.92341592    0.13108478    0.20212832     408
## 5           ub11  0.57200207 -0.01869151    0.04879409    0.13071184      32
## 6           ub13  0.31025514  0.56073473    0.90121486    1.11629844     648
## 7           ub14  0.23241080 -0.06857532    1.50094682   -0.27687601     333
## 8           ub15  0.67870908  0.25359928    0.61393136    0.03100248      36
## 9           ub18  1.08814254  0.77816500    0.52245709   -0.31888702     552
## 10          ub24  0.55318776  0.08084087    0.28256540    0.03038869     275
## 11          ub25  0.91751740  0.28109140    0.41293602    0.58451878      70
## 12          ub27  0.68424700  0.51216459    0.44334149    0.98690632     496
## 13          ub28  1.08844176  0.55663600    0.77617187    0.93836953       0
## 14          ub29  0.94430311  0.21879167    0.76616645    0.40828505     476
## 15          ub31 -0.15051656  0.64307684    1.20427060    0.88612653     363
## 16          ub32 -0.01583277  0.90497541    0.89984149    0.99438842       0
## 17          ub34  0.53679315  0.57458515    0.26553538    0.00799229     198
## 18          ub35  0.89884164 -0.12045279    0.94869928    1.19622739       0
## 19          ub36  0.63866504  0.64107173    0.36672662    0.62092800     216
## 20          ub38 -0.23209723  0.13055599   -0.20514602   -0.07578923     240
## 21          ub40  0.35954000  0.30356801    0.51371306    0.50089370     450
## 22          ub41  0.26726994 -0.24029660    0.35867739    0.02753136     684
## 23          ub42  0.84482587 -0.37601343    0.63582486   -0.55788997     836
## 24          ub43  0.63319883  0.99021393    0.56643958    0.38961081     506
## 25          ub44  0.43954561  0.62261116    0.93824426    0.98645892       0
## 26          ub45  0.73144877  0.16658792    1.08670619    0.86611864     418
## 27          ub46 -0.07735156  0.05022121    0.08362815   -0.10227978     480
## 28          ub47  1.08893601  1.22970000   -0.18741515    0.16271922      50
## 29          ub48  0.79863715  0.89204287    1.32229673    0.62636312       0
## 30          ub49  0.44411896  0.16209957    0.33019024    0.33564519     336
## 31          ub50  0.53631389  0.68478850    0.15458906    0.28350526     224
##    cued_differential uncued_differential diff_bias_change
## 1        0.371668148         -0.07323769       0.44490584
## 2       -0.359624732          0.65466998      -1.01429471
## 3       -0.192821041          0.81248335      -1.00530440
## 4       -0.570273967         -0.07104354      -0.49923043
## 5        0.590693579         -0.08191775       0.67261133
## 6       -0.250479588         -0.21508358      -0.03539601
## 7        0.300986111          1.77782283      -1.47683672
## 8        0.425109806          0.58292888      -0.15781907
## 9        0.309977544          0.84134411      -0.53136657
## 10       0.472346888          0.25217670       0.22017019
## 11       0.636426002         -0.17158276       0.80800876
## 12       0.172082405         -0.54356483       0.71564723
## 13       0.531805761         -0.16219766       0.69400342
## 14       0.725511445          0.35788139       0.36763005
## 15      -0.793593406          0.31814407      -1.11173748
## 16      -0.920808186         -0.09454693      -0.82626125
## 17      -0.037791997          0.25754309      -0.29533509
## 18       1.019294435         -0.24752811       1.26682255
## 19      -0.002406687         -0.25420138       0.25179469
## 20      -0.362653222         -0.12935678      -0.23329644
## 21       0.055971988          0.01281936       0.04315263
## 22       0.507566544          0.33114603       0.17642051
## 23       1.220839300          1.19371483       0.02712447
## 24      -0.357015106          0.17682877      -0.53384387
## 25      -0.183065550         -0.04821467      -0.13485088
## 26       0.564860845          0.22058754       0.34427330
## 27      -0.127572774          0.18590793      -0.31348070
## 28      -0.140763994         -0.35013437       0.20937037
## 29      -0.093405722          0.69593361      -0.78933933
## 30       0.282019388         -0.00545494       0.28747433
## 31      -0.148474613         -0.12891620      -0.01955842

Now that we have the values for the axes, I’m going to plot the graph.

Plot the graph

We select the data to be what we used before, then we specify the y and x axes. We then use geom_point to create a scatter plot. After that, we add geom_smooth to add a regression line. Within geom_smooth we have to add method = lm to make it a straight line, and we get rid of the confidence interval with se = F.

ggplot(data = differential, aes(
  x = SWSxREM,
  y = diff_bias_change
))+
  geom_point()+
  geom_smooth(method = lm, #lm stands for linear model, so it makes the line straight
              se = F)+ #removes confidence interval shading
  xlim(0,1000) #for some reason the x axis isn't starting at 0 with this
## `geom_smooth()` using formula 'y ~ x'

An attempt to get the x-axis to start at 0. I found this solution on Google which adds the scale_x_continuous function to centre the graph at the origin. At the same time, the `limits = c(0,1000) allows us to expand the axis to 1000, the same length as the original graph.

Final graph:

ggplot(data = differential, aes(
  x = SWSxREM,
  y = diff_bias_change
))+
  geom_point()+
  geom_smooth(method = lm, #lm stands for linear model, so it makes the line straight
              se = F)+ #removes confidence interval shading
  scale_x_continuous(expand = c(0,0),limits = c(0,1000))+ 
  scale_y_continuous(expand = c(0,0),limits = c(-2,1.5))+
  labs(title = "Fig 5. No association between minutes in SWS x minutes in REM and differential bias change", 
       x = "SWS x REM sleep duration (min)",
       y = "Differential bias change")+
  theme_bw()
## `geom_smooth()` using formula 'y ~ x'