I am working on visual representations of single case studies. I need to make some changes to my graph in ggplot2, but I found this to be a bit challenging. Here is a brief description of the variables contained in the toy data set that I used to make a reproducible example:
- Occasion: Number of the session rater evaluated the behavior (from 1 to n);
- Time: Number of each condition (baseline from 1 to n and intervention from 1 to n);
- Phase: Condition (A = baseline or B = intervention);
- ID: student code in the study;
- Outcome: total score on a behavioral checklist.
library(dplyr)
library(ggplot2)
db_tb <- read.table(header = TRUE, text = '
Occasion Time Phase ID outcome
1 1 A 1 15
2 2 A 1 14
3 3 A 1 8
4 4 A 1 10
5 5 A 1 14
6 6 A 1 8
7 7 A 1 10
8 1 B 1 21
9 2 B 1 23
10 3 B 1 24
11 4 B 1 20
12 5 B 1 25
13 6 B 1 15
14 7 B 1 11
15 8 B 1 23
16 9 B 1 20
17 10 B 1 NA
18 11 B 1 15
19 12 B 1 20
20 13 B 1 NA
21 14 B 1 16
1 1 A 2 18
2 2 A 2 14
3 3 A 2 18
4 4 A 2 21
5 1 B 2 8
6 2 B 2 NA
7 3 B 2 10
8 4 B 2 17
9 5 B 2 NA
10 6 B 2 29
1 1 A 3 15
2 2 A 3 7
3 3 A 3 14
4 1 B 3 15
5 2 B 3 14
6 3 B 3 11
7 4 B 3 10
8 5 B 3 NA
9 6 B 3 NA
10 7 B 3 7
11 8 B 3 9
12 9 B 3 13
13 10 B 3 11
')
In order to separate the baseline and intervention, I created vlines_tb that gives me a table with the session number after which I will set the vertical line in ggplot2.
#table containing the last day of the baseline phase
vlines_tb <- db_tb %>%
filter(Phase == "A") %>%
group_by(ID, Phase) %>%
summarise(y = max(Occasion))
Finally I created the graph consistently with other papers in the field.
#create a visual representation
db_tb %>%
na.omit(outcome) %>% #Interpolate missing data so all markers within phase are connected
ggplot(aes(x = Occasion, y = outcome, group = Phase)) +
geom_point(size = 1.8) +
geom_line(size = 0.65) +
ggtitle("Baseline") +
facet_grid(ID ~ .) +
scale_x_continuous(name = "Occasions", breaks = seq(0, 70, 5)) +
scale_y_continuous(name = "Rating", limits = c(0, 30)) +
theme_classic() +
theme(strip.background = element_blank(),
axis.title.x = element_text(margin = margin(t = 20, r = 0, b = 0, l = 0)),
axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0))) +
annotate("segment", x = -Inf, xend = Inf, y = -Inf, yend = -Inf) +
geom_vline(data = vlines_tb, aes(xintercept = y + 0.5), colour = "black", linetype = "dashed")
However, I would like to make a couple of changes:
- Sort the subjects based on when the intervention was implemented so that the subject on the top would be the one who received the intervention first, etc.; and
- Re-label the IDs so that the first one from the top represented in the graph would be renamed as 1, etc.;
- Label baseline and intervention conditions at the top of the chart (they should be above the two corresponding areas).
I made the changes through Excel to show what the final outcome should look like (see below). Thanks for any help!