Documentation for fxl

Drawing Functional Analyses in R: Recreation of Gilroy et al. (2019)

Written by Shawn P. Gilroy (Last Updated: 2024-06-02)Functional AnalysisMultielement Designs

This short tutorial focuses on drawing the Iwata et al. (1984/1992) functional analysis–a hallmark methodology with a well-defined style and format. The figure and data for this example is derived from Gilroy et al. (2021), a study that matched a Functional Communication Training intervention to functions identified via functional analysis.

The final figure for this work is illustrated below:

Data Structure

Data for this study focused on a single (note: CTB = combined rates of target behavior rates) change measure–CTB. This is a relatively simple plot, because we can use a single series and just style the phase markers in a specific manner.

A snapshot of the relevant data is shown below:


head(csv_data)
##   X Session SIB AGG DIS Prompt Comply    SR CTB Condition
## 1 1       1   0 0.0 0.0    0.0    0.0  0.00 0.0  Tangible
## 2 2       2   0 0.0 0.0    0.0    0.0  0.00 0.0  Toy Play
## 3 3       3   0 0.0 0.3    0.0    0.0  0.60 0.3 Attention
## 4 4       4   0 0.0 0.0    2.1    1.2 30.05 0.0    Demand
## 5 5       5   0 0.2 0.7    0.6    0.2  0.00 0.9    Demand
## 6 6       6   0 0.0 0.0    0.0    0.0  0.00 0.0  Tangible

Plot Elements

Not much customization is needed for this type of plot, most of the work is focused on using keyed lists to style features for each phase/condition.

The unmodified raw output of the data is presented below:


scr_plot(csv_data,
         aesthetics = var_map(x = Session,
                              y = CTB,
                              p = Condition),
         mai = c(0.5, 0.5, 0.1, 0.5),
         omi = c(0.25, 0.25, 0.25, 0.25),
         family = "Times New Roman") |>
  scr_xoverride(c(-.5, 15)) |>
  scr_yoverride(c(-.05, 2),
                yticks = c(0, 0.5, 1, 1.5, 2),
                ytickslabs = c(
                  "0",
                  "0.5",
                  "1",
                  "1.5",
                  "2"
                )) |> 
  scr_lines(size = 1) |>
  scr_points(cex = 2)

Axis and Tick Styling

The axis titles and plot title are a bit sparse in detail, and the figure can be made more informative by added detail out a bit.


scr_plot(csv_data,
         aesthetics = var_map(x = Session,
                              y = CTB,
                              p = Condition),
         mai = c(0.5, 0.5, 0.1, 0.5),
         omi = c(0.25, 0.25, 0.25, 0.25),
         family = "Times New Roman") |>
  scr_xoverride(c(-.5, 15)) |>
  scr_yoverride(c(-.05, 2),
                yticks = c(0, 0.5, 1, 1.5, 2),
                ytickslabs = c(
                  "0",
                  "0.5",
                  "1",
                  "1.5",
                  "2"
                )) |> 
  scr_lines(size = 1) |>
  scr_points(cex = 2) |>
  scr_xlabel("Session",
             adj = 0.55,
            cex = 1.15) |>
  scr_ylabel("Combined Target Behavior (Per Minute)",
             cex = 1.15,
             adj = 0.65) |>
  scr_title("Analog Functional Analysis")

Keyed Phase Styles for Markers

The various phases for markers can be keyed in to adjust various features. Under-the-hood, fxl uses the same color and marker conventions (it is base R after all). The shapes for markers can be overridden using the pch argument and the fill parameter can be used (with appropriate shapes) to set the color filling the marker.


scr_plot(csv_data,
         aesthetics = var_map(x = Session,
                              y = CTB,
                              p = Condition),
         mai = c(0.5, 0.5, 0.1, 0.5),
         omi = c(0.25, 0.25, 0.25, 0.25),
         family = "Times New Roman") |>
  scr_xoverride(c(-.5, 15)) |>
  scr_yoverride(c(-.05, 2),
                yticks = c(0, 0.5, 1, 1.5, 2),
                ytickslabs = c(
                  "0",
                  "0.5",
                  "1",
                  "1.5",
                  "2"
                )) |> 
  scr_lines(size = 1) |>
  scr_points(cex = 2,
             pch = list("Toy Play" = 16,
                        "Attention" = 22,
                        "Demand" = 24,
                        "Tangible" = 8),
             fill = list("Toy Play" = "black",
                         "Attention" = "white",
                         "Demand" = "white",
                         "Tangible" = "black")) |>
  scr_xlabel("Session",
             adj = 0.55,
            cex = 1.15) |>
  scr_ylabel("Combined Target Behavior (Per Minute)",
             cex = 1.15,
             adj = 0.65) |>
  scr_title("Analog Functional Analysis")

Figure Legend

The figure is made complete by adding in the legend, consistent with the approach used in the prior plot.

With this–the functional analysis figure is complete!


scr_plot(csv_data,
         aesthetics = var_map(x = Session,
                              y = CTB,
                              p = Condition),
         mai = c(0.5, 0.5, 0.1, 0.5),
         omi = c(0.25, 0.25, 0.25, 0.25),
         family = "Times New Roman") |>
  scr_xoverride(c(-.5, 15)) |>
  scr_yoverride(c(-.05, 2),
                yticks = c(0, 0.5, 1, 1.5, 2),
                ytickslabs = c(
                  "0",
                  "0.5",
                  "1",
                  "1.5",
                  "2"
                )) |> 
  scr_lines(size = 1) |>
  scr_points(cex = 2,
             pch = list("Toy Play" = 16,
                        "Attention" = 22,
                        "Demand" = 24,
                        "Tangible" = 8),
             fill = list("Toy Play" = "black",
                         "Attention" = "white",
                         "Demand" = "white",
                         "Tangible" = "black")) |>
  scr_xlabel("Session",
             adj = 0.55,
            cex = 1.15) |>
  scr_ylabel("Combined Target Behavior (Per Minute)",
             cex = 1.15,
             adj = 0.65) |>
  scr_title("Analog Functional Analysis") |>
  scr_legend(position = "topright",
             legend = c("Toy Play",
                        "Attention",
                        "Demand",
                        "Tangible"),
             col = c("black",
                     "black",
                     "black",
                     "black"),
             pt_bg = c( "black", 
                        "white", 
                        "white", 
                        "black"),
             lty = c(1,
                     1,
                     1,
                     1),
             pch = c(16,
                     22,
                     24,
                     8),
             bty = "n",
             pt_cex = 2,
             cex = 1.125,
             text_col = "black",
             horiz = FALSE,
             box_lty = 0)