Feature Plots

knitr::opts_chunk$set(echo = TRUE)
options(crayon.enabled = FALSE, cli.num_colors = 0)

Download a copy of the vignette to follow along here: feature_plots.Rmd

Given a cluster solution formatted as a row of a solutions data frame (or extended solutions data frame) and a data list containing features to plot, the auto_plot() function can automatically generate ggplot-based bar and jitter plots showing how that particular feature was divided across clusters.

library(metasnf)

dl <- data_list(
    list(subc_v, "subcortical_volume", "neuroimaging", "continuous"),
    list(income, "household_income", "demographics", "continuous"),
    list(fav_colour, "favourite_colour", "misc", "categorical"),
    list(pubertal, "pubertal_status", "demographics", "continuous"),
    list(anxiety, "anxiety", "behaviour", "ordinal"),
    list(depress, "depressed", "behaviour", "ordinal"),
    uid = "unique_id"
)

# Build space of settings to cluster over
set.seed(42)
sc <- snf_config(
    dl = dl,
    n_solutions = 2,
    min_k = 20,
    max_k = 50
)

# Clustering
sol_df <- batch_snf(dl, sc)

sol_df_row <- sol_df[1, ]

The row you pick could come from a solutions_df or ext_solutions_df class object.

plot_list <- auto_plot(
    sol_df_row = sol_df_row,
    dl = dl,
    verbose = TRUE
)

plot_list$"household_income"

plot_list$"smri_vol_scs_csf"

plot_list$"colour"

If there's something you'd like to change about the plot, you can always tack on ggplot2 elements to build from the skeleton provided by auto_plot:

plot_list$"colour" +
    ggplot2::labs(
        fill = "Favourite Colour",
        x = "Cluster",
        title = "Favourite Colour by Cluster"
    ) +
    ggplot2::scale_fill_manual(
        values = c(
            "green" = "forestgreen",
            "red" = "firebrick3",
            "yellow" = "darkgoldenrod1"
        )
    )


Try the metasnf package in your browser

Any scripts or data that you put into this service are public.

metasnf documentation built on April 3, 2025, 5:40 p.m.