Visualizations in ALASCA

We have aimed to implement a range of plotting options for ALASCA. In general, the plotting function is

plot(<object>, component = <component>, effect = <effect>, type = <type>, ...)

where <object> is your ALASCA model, <component> and <effect> the components and effects of interest, and <type> the plot type.

ALASCA can save the plots directly if you specify save = TRUE when you initialize the ALASCA model. Otherwise, the plots are returned as ordinary ggplot2 objects and can be saved with ggsave().

Note that you can specify properties of the plot during plotting, e.g. specifying palette:

plot(res, component = 1, effect = 1, type = 'effect', palette = c("red", "blue"))
#> INFO  [2024-09-13 12:51:29] Effect plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

The effect plot

The effect plot is the default plot showing both scores and loadings for the selected components/effects. You can select one effect and one component:

plot(res, component = 1, effect = 1, type = 'effect')
#> INFO  [2024-09-13 12:51:30] Effect plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

Or two components:

plot(res, component = c(1, 2), effect = 1, type = 'effect')
#> INFO  [2024-09-13 12:51:31] Effect plot. Selected effect (nr 1): `time+time:group+group`. Component: 1 and 2.

Or two effects (note: this requires a model with multiple effects):

plot(res_2, component = 1, effect = c(1, 2), type = 'effect')
#> INFO  [2024-09-13 12:51:32] Effect plot. Selected effect (nr 1 and 2): `time` and `time:group+group`. Component: 1.

Or even a combination of multiple components and effects:

plot(res_2, component = c(1,2), effect = c(1, 2), type = 'effect')
#> INFO  [2024-09-13 12:51:34] Effect plot. Selected effect (nr 1 and 2): `time` and `time:group+group`. Component: 1 and 2.

Plotting scores or loadings

The effect plot consists of two parts: scores and loadings. You can get those components directly:

plot(res, component = 1, effect = 1, type = 'score')
#> INFO  [2024-09-13 12:51:36] Score plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

And similarly for loadings:

plot(res, component = 1, effect = 1, type = 'loading')
#> INFO  [2024-09-13 12:51:36] Loading plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

Grayscale

Some journals require plots in grayscale (or require fees for colors). You can therefore ask for grayscale figures in ALASCA:

res_3 <- ALASCA(
  df,
  value ~ time*group + (1|id),
  plot.grayscale = TRUE
)
#> INFO  [2024-09-13 12:51:37] Initializing ALASCA (v1.0.17, 2024-09-13)
#> WARN  [2024-09-13 12:51:37] Guessing effects: `time+time:group+group`
#> INFO  [2024-09-13 12:51:37] Will use linear mixed models!
#> INFO  [2024-09-13 12:51:37] Will use Rfast!
#> WARN  [2024-09-13 12:51:37] The `group` column is used for stratification
#> WARN  [2024-09-13 12:51:37] Converting `character` columns to factors
#> INFO  [2024-09-13 12:51:37] Scaling data with sdall ...
#> INFO  [2024-09-13 12:51:37] Calculating LMM coefficients
#> INFO  [2024-09-13 12:51:37] ==== ALASCA has finished ====
#> INFO  [2024-09-13 12:51:37] To visualize the model, try `plot(<object>, effect = 1, component = 1, type = 'effect')`
plot(res_3, component = 1, effect = 1, type = 'effect')
#> INFO  [2024-09-13 12:51:37] Effect plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

Adapting the theme

You can specify theme etc. of the plots when you initialize the ALASCA model:

res_4 <- ALASCA(
  df,
  value ~ time*group + (1|id),
  plot.my_theme = theme_linedraw(),
  plot.palette = c("red", "blue")
)
#> INFO  [2024-09-13 12:51:38] Initializing ALASCA (v1.0.17, 2024-09-13)
#> WARN  [2024-09-13 12:51:38] Guessing effects: `time+time:group+group`
#> INFO  [2024-09-13 12:51:38] Will use linear mixed models!
#> INFO  [2024-09-13 12:51:38] Will use Rfast!
#> WARN  [2024-09-13 12:51:38] The `group` column is used for stratification
#> WARN  [2024-09-13 12:51:38] Converting `character` columns to factors
#> INFO  [2024-09-13 12:51:38] Scaling data with sdall ...
#> INFO  [2024-09-13 12:51:38] Calculating LMM coefficients
#> INFO  [2024-09-13 12:51:38] ==== ALASCA has finished ====
#> INFO  [2024-09-13 12:51:38] To visualize the model, try `plot(<object>, effect = 1, component = 1, type = 'effect')`
plot(res_4, component = 1, effect = 1, type = 'effect')
#> INFO  [2024-09-13 12:51:38] Effect plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

The color palette is by default made from the viridis palette, ending at 0.8: scales::viridis_pal(end = 0.8)(2). You can of course use the colors you want;

res_5 <- ALASCA(
  df,
  value ~ time*group + (1|id),
  plot.my_theme = theme_light(),
  validate = TRUE,
  n_validation_runs = 10,
  plot.palette = scales::brewer_pal(type = "qual")(2)
)
plot(res_5, component = 1, effect = 1, type = 'effect')

To remove the ribbon, you can set plot.ribbon = FALSE:

res_5 <- ALASCA(
  df,
  value ~ time*group + (1|id),
  plot.my_theme = theme_light(),
  validate = TRUE,
  n_validation_runs = 10,
  plot.palette = scales::brewer_pal(type = "qual")(2),
  plot.ribbon = FALSE
)
#> INFO  [2024-09-13 12:51:42] Initializing ALASCA (v1.0.17, 2024-09-13)
#> WARN  [2024-09-13 12:51:42] Guessing effects: `time+time:group+group`
#> INFO  [2024-09-13 12:51:42] Will use linear mixed models!
#> INFO  [2024-09-13 12:51:42] Will use Rfast!
#> WARN  [2024-09-13 12:51:42] The `group` column is used for stratification
#> WARN  [2024-09-13 12:51:42] Converting `character` columns to factors
#> INFO  [2024-09-13 12:51:42] Scaling data with sdall ...
#> INFO  [2024-09-13 12:51:42] Calculating LMM coefficients
#> INFO  [2024-09-13 12:51:42] Starting validation: bootstrap
#> INFO  [2024-09-13 12:51:42] - Run 1 of 10
#> INFO  [2024-09-13 12:51:42] --- Used 0.22 seconds. Est. time remaining: 2.02 seconds
#> INFO  [2024-09-13 12:51:42] - Run 2 of 10
#> INFO  [2024-09-13 12:51:42] --- Used 0.12 seconds. Est. time remaining: 1.37 seconds
#> INFO  [2024-09-13 12:51:42] - Run 3 of 10
#> INFO  [2024-09-13 12:51:42] --- Used 0.11 seconds. Est. time remaining: 1.07 seconds
#> INFO  [2024-09-13 12:51:42] - Run 4 of 10
#> INFO  [2024-09-13 12:51:42] --- Used 0.12 seconds. Est. time remaining: 0.87 seconds
#> INFO  [2024-09-13 12:51:42] - Run 5 of 10
#> INFO  [2024-09-13 12:51:43] --- Used 0.12 seconds. Est. time remaining: 0.7 seconds
#> INFO  [2024-09-13 12:51:43] - Run 6 of 10
#> INFO  [2024-09-13 12:51:43] --- Used 0.12 seconds. Est. time remaining: 0.55 seconds
#> INFO  [2024-09-13 12:51:43] - Run 7 of 10
#> INFO  [2024-09-13 12:51:43] --- Used 0.12 seconds. Est. time remaining: 0.4 seconds
#> INFO  [2024-09-13 12:51:43] - Run 8 of 10
#> INFO  [2024-09-13 12:51:43] --- Used 0.13 seconds. Est. time remaining: 0.27 seconds
#> INFO  [2024-09-13 12:51:43] - Run 9 of 10
#> INFO  [2024-09-13 12:51:43] --- Used 0.16 seconds. Est. time remaining: 0.14 seconds
#> INFO  [2024-09-13 12:51:43] - Run 10 of 10
#> INFO  [2024-09-13 12:51:43] --- Used 0.14 seconds. Est. time remaining: 0 seconds
#> INFO  [2024-09-13 12:51:43] Calculating percentiles for score and loading
#> INFO  [2024-09-13 12:51:43] ==== ALASCA has finished ====
#> INFO  [2024-09-13 12:51:43] To visualize the model, try `plot(<object>, effect = 1, component = 1, type = 'effect')`
plot(res_5, component = 1, effect = 1, type = 'effect')
#> INFO  [2024-09-13 12:51:43] Effect plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

You can also change these properties directly in the plot() function:

plot(res_5,
     component = 1,
     effect = 1,
     type = 'effect',
     ribbon = TRUE,
     palette = scales::hue_pal()(2),
     my_theme = theme_classic())
#> INFO  [2024-09-13 12:51:44] Effect plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

or (x_angle controls the rotation of the x axis labels, and x_h_just is used to center them horizontally)

plot(res_5,
     component = 1,
     effect = 1,
     type = 'effect',
     grayscale = TRUE,
     x_angle = 0, x_h_just= 0.5
)
#> INFO  [2024-09-13 12:51:45] Effect plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

Some even prefer to have loadings along the x axis:

plot(res_5,
     component = 1,
     effect = 1,
     type = 'effect',
     x_angle = 90, x_h_just = 1, x_v_just = 0.5,
     flip_axis= FALSE
)
#> INFO  [2024-09-13 12:51:46] Effect plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

Screeplot

Note: The plotting function remembers previous choices, so if you plotted component 1 in the previous plot, you have to specify which components to plot:

plot(res, component = seq(1,10), effect = 1, type = 'scree')
#> INFO  [2024-09-13 12:51:47] Scree plot. Selected effect (nr 1): `time+time:group+group`. Component: 1 and 2 and 3 and 4 and 5 and 6 and 7 and 8 and 9 and 10.

Predictions / Marginal means

By default, only some variables are selected based on their loadings:

plot(res, component = 1, effect = 1, type = 'prediction')
#> INFO  [2024-09-13 12:51:47] Prediction plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.
#> INFO  [2024-09-13 12:51:47] Selecting the 0 variables with highest/lowest loading on `time+time:group+group` (PC1). Use `variable` to specify variables to plot

You can ask for specific variables:

plot(res, component = 1, effect = 1, type = 'prediction', variable = c("variable_8", "variable_13", "variable_15"))
#> INFO  [2024-09-13 12:51:49] Prediction plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.

Or adjust the number of retrieved variables (note that we have to set variable = c() for the plot function to forget the previous setting):

plot(res, component = 1, effect = 1, type = 'prediction', n_limit = 4, variable = c())
#> INFO  [2024-09-13 12:51:50] Prediction plot. Selected effect (nr 1): `time+time:group+group`. Component: 1.
#> INFO  [2024-09-13 12:51:50] Selecting the 2 variables with highest/lowest loading on `time+time:group+group` (PC1). Use `variable` to specify variables to plot

Histogram

Visualize the distribution of the bootstrap runs:

res_6 <- ALASCA(
  df,
  value ~ time*group + (1|id),
  validate = TRUE,
  n_validation_runs = 100
)
plot(res_6, component = 1, effect = 1, type = 'histogram', n_bins = 30)