View source: R/helper_plot_functions.R
| plot_spaghetti | R Documentation |
A helper function to plot spaghetti plots of continuous variables separated
by the clusters found by longmixr.
plot_spaghetti(
model,
data,
variable_names,
time_variable,
show_mean_sd_ribbon = TRUE,
number_of_clusters = 2,
scales = "fixed"
)
model |
|
data |
a |
variable_names |
character vector of the continuous variables to be plotted |
time_variable |
the name of the variable that depicts the time point of the measurements |
show_mean_sd_ribbon |
|
number_of_clusters |
the number of clusters that should be plotted, the
default is |
scales |
|
The spaghetti plot shows the longitudinal trajectory (defined by
time_variable) of continuous variables separated by the clusters found
by longitudinal_consensus_cluster. The provided data.frame
for data can either be the same as used in the clustering with
longitudinal_consensus_cluster or needs to contain the same
id_column as in the clustering and a time_variable.
a ggplot object that is plotted
set.seed(5)
test_data <- data.frame(patient_id = rep(1:10, each = 4),
visit = rep(1:4, 10),
var_1 = c(rnorm(20, -1), rnorm(20, 3)) +
rep(seq(from = 0, to = 1.5, length.out = 4), 10),
var_2 = c(rnorm(20, 0.5, 1.5), rnorm(20, -2, 0.3)) +
rep(seq(from = 1.5, to = 0, length.out = 4), 10))
model_list <- list(flexmix::FLXMRmgcv(as.formula("var_1 ~ .")),
flexmix::FLXMRmgcv(as.formula("var_2 ~ .")))
clustering <- longitudinal_consensus_cluster(
data = test_data,
id_column = "patient_id",
max_k = 2,
reps = 3,
model_list = model_list,
flexmix_formula = as.formula("~s(visit, k = 4) | patient_id"))
plot_spaghetti(
model = clustering,
data = test_data,
variable_names = "var_1",
time_variable = "visit"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.