knitr::opts_chunk$set(echo = TRUE)
Note: This vignette is illustrated with fake data. The dataset explored in this example should not be used to inform decision-making.
library(ready4) library(ready4use)
ready4use includes a number of tools for visualising health economic model data and forms part of the ready4 framework. Details of how to find compatible datasets are provided in another article. The ready4use visualisation tools illustrated in this vignette provide an interface to functions from the ggpubr library. To work as intended, some (but not most) of these tools also require the ggpubr library to be loaded.
library(ggpubr)
We begin by ingesting the data from online repository we require into a Ready4useDyad
, as illustrated in another vignette.
objects_ls <- Ready4useRepos(dv_nm_1L_chr = "fakes", dv_ds_nm_1L_chr = "https://doi.org/10.7910/DVN/HJXYKQ", dv_server_1L_chr = "dataverse.harvard.edu") %>% ingest(fls_to_ingest_chr = c("ymh_clinical_tb","ymh_clinical_dict_r3"), metadata_1L_lgl = F) X <- Ready4useDyad(ds_tb = objects_ls$ymh_clinical_tb, dictionary_r3 = objects_ls$ymh_clinical_dict_r3) %>% renew(type_1L_chr = "case")
This dataset is a synthetic ("fake") microdata representation of clinic patients with data for each patient reported for up to two data collection rounds (baseline and follow-up). We also need to transform this dataset into other formats to use with some of the plot types we illustrate in this vignette. We first make a dataset with baseline values only.
X1 <- renewSlot(X, "ds_tb", procureSlot(X, "ds_tb") %>% dplyr::filter(round=="Baseline"))
Next, we create summary datasets with mean variable values for the entire sample at baseline.
X2 <- renewSlot(X1, "ds_tb", procureSlot(X, "ds_tb") %>% dplyr::group_by(d_studying_working) %>% dplyr::summarise(dplyr::across(dplyr::where(is.numeric), function(x){ mean(x, na.rm = TRUE)}))) X3 <- renewSlot(X1, "ds_tb", procureSlot(X, "ds_tb") %>% dplyr::group_by(d_studying_working, d_sex_birth_s) %>% dplyr::summarise(dplyr::across(dplyr::where(is.numeric), function(x){ mean(x, na.rm = TRUE)}), .groups = 'drop')) X4 <- renewSlot(X1, "ds_tb", procureSlot(X, "ds_tb") %>% dplyr::group_by(d_sex_birth_s, d_country_bir_s, d_studying_working, c_p_diag_s,c_clinical_staging_s) %>% dplyr::summarise(dplyr::across(dplyr::where(is.numeric), function(x){ mean(x, na.rm = TRUE)}), .groups = 'drop'))
We also create datasets with summaries of mean variable values at both timepoints.
X5 <- renewSlot(X, "ds_tb", procureSlot(X, "ds_tb") %>% dplyr::group_by(round) %>% dplyr::summarise(dplyr::across(dplyr::where(is.numeric), function(x){ mean(x, na.rm = TRUE)}))) X6 <- renewSlot(X, "ds_tb", procureSlot(X, "ds_tb") %>% dplyr::group_by(round, d_sex_birth_s) %>% dplyr::summarise(dplyr::across(dplyr::where(is.numeric), function(x){ mean(x, na.rm = TRUE)})))
We make a dataset that is restricted to 50 randomly selected cases for which data is available at two timepoints.
X7 <- renewSlot(X, "ds_tb", procureSlot(X, "ds_tb") %>% dplyr::filter(fkClientID %in% (intersect(procureSlot(X, "ds_tb") %>% dplyr::filter(round=="Follow-up") %>% dplyr::pull(fkClientID), X1@ds_tb$fkClientID) %>% sample(50))))
We next make dataset summaries of the counts at baseline of sample sub-groups.
X8 <- renewSlot(X1, "ds_tb", procureSlot(X1, "ds_tb") %>% dplyr::filter(!is.na(d_studying_working)) %>% dplyr::group_by(d_studying_working) %>% dplyr::summarise(Count = dplyr::n())) X9 <- renewSlot(X1, "ds_tb", table(procureSlot(X1, "ds_tb") %>% dplyr::select(d_studying_working, d_sex_birth_s)) %>% tibble::as_tibble()) X10 <- renewSlot(X1, "ds_tb", table(procureSlot(X1, "ds_tb") %>% dplyr::select(d_studying_working, d_sex_birth_s, c_p_diag_s, c_clinical_staging_s)) %>% tibble::as_tibble())
Some of the visualisations that we will be generating in this vignette will require labels that are derived from variable values to be modified (e.g. for brevity). We therefore next make look-up tables for recoding these values.
x <- renew(ready4show::ready4show_correspondences(), old_nms_chr = c("Not studying or working", "Studying only", "Studying and working", "Working only", "Female", "Male"), new_nms_chr = c("NEITHER", "EDUCATION", "BOTH", "EMPLOYMENT", "FEMALE", "MALE")) y <- renew(ready4show::ready4show_correspondences(), old_nms_chr = c("Not studying or working", "Studying only", "Studying and working", "Working only", "Depression and Anxiety"), new_nms_chr = c("Neither", "Education", "Both", "Employment", "Both"))
Data contained in a Ready4useDyad can be visualised using the Depict
method.
depict(X1, x_vars_chr = "d_studying_working")
depict(X1, x_vars_chr = "d_studying_working", drop_missing_1L_lgl = T)
depict(X1, x_vars_chr = "d_studying_working", drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T)
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = NA_character_, y_labels_chr = "", z_labels_chr = NA_character_, drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T)
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_labels_chr = "", z_labels_chr = "", drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T)
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_labels_chr = "", z_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T)
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_labels_chr = "", z_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T, recode_lup_r3 = x)
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_labels_chr = "", z_vars_chr = "d_sex_birth_s", z_labels_chr = "SEX", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, recode_lup_r3 = x)
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_labels_chr = "", z_vars_chr = "d_sex_birth_s", z_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, position_xx = ggplot2::position_stack(), recode_lup_r3 = y)
depict(X2, x_vars_chr = "d_studying_working", x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = NA_character_, z_labels_chr = "", drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T, recode_lup_r3 = x, titles_chr = NA_character_)
depict(X3, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_vars_chr = "k6_total", y_labels_chr = "K6", z_vars_chr = "d_sex_birth_s", z_labels_chr = "SEX", drop_missing_1L_lgl = T, titles_chr = "PSYCHOLOGICAL DISTRESS BY EDUCATION, EMPLOYMENT AND SEX")
The palette options from the "ggsci" package that are compatible with the ready4use Depict method can be retrieved using the following function call.
get_styles("ggsci")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_labels_chr = "", z_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T, style_1L_chr = "lancet")
The palette options from "the viridis" package that are compatible with the ready4use Depict method can be retrieved using the following function call.
get_styles("viridis")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_labels_chr = "", z_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T, style_1L_chr = "rocket", type_1L_chr = "viridis")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_labels_chr = "", z_labels_chr = "", as_percent_1L_lgl = T, colours_chr = c("#130d9c","#9c0d39"), drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T, type_1L_chr = "manual")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "", y_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, fill_single_1L_lgl = T, recode_lup_r3 = x, titles_chr = "EDUCATION AND EMPLOYMENT STATUS")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "", y_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, fill_single_1L_lgl = T, recode_lup_r3 = x, style_1L_chr = "aaas", titles_chr = "EDUCATION AND EMPLOYMENT STATUS")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "", y_labels_chr = "", as_percent_1L_lgl = T, colours_chr = "black", drop_missing_1L_lgl = T, fill_single_1L_lgl = T, recode_lup_r3 = x, titles_chr = "EDUCATION AND EMPLOYMENT STATUS", type_1L_chr = "manual")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "", y_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, fill_single_1L_lgl = T, recode_lup_r3 = x, titles_chr = "EDUCATION AND EMPLOYMENT STATUS", label = T, lab.pos = "in", lab.col = "white")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "", y_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, fill_single_1L_lgl = T, recode_lup_r3 = x, titles_chr = "EDUCATION AND EMPLOYMENT STATUS", orientation = "horiz")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "", y_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, fill_single_1L_lgl = T, recode_lup_r3 = x, titles_chr = "EDUCATION AND EMPLOYMENT STATUS", order = c("EDUCATION", "EMPLOYMENT","BOTH", "NEITHER"))
plot_ls <- depict(X1, x_vars_chr = c("d_sex_birth_s","d_studying_working", "d_country_bir_s"), x_labels_chr = NA_character_, y_labels_chr = "" , z_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T)
plot_ls$d_sex_birth_s
plot_ls$d_studying_working
plot_ls$d_country_bir_s
depict(X4, x_vars_chr = c("d_country_bir_s", "d_studying_working", "c_p_diag_s", "c_clinical_staging_s"), x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = "K6", z_vars_chr = "d_sex_birth_s", z_labels_chr = "SEX", arrange_1L_lgl = T, arrange_args_ls = list(ncol = 2, nrow = 2, common.legend = T), drop_missing_1L_lgl = T, recode_lup_r3 = y)
depict(X1, x_vars_chr = "k6_total", what_1L_chr = "density")
depict(X1, x_vars_chr = "k6_total", x_labels_chr = NA_character_, y_labels_chr = "Percentatge", z_vars_chr = "d_studying_working", z_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, what_1L_chr = "density")
depict(X1, x_vars_chr = "k6_total", what_1L_chr = "ecdf")
depict(X1,x_vars_chr = "k6_total", x_labels_chr = NA_character_, y_labels_chr = "", z_vars_chr = "d_studying_working", z_labels_chr = "", drop_missing_1L_lgl = T, what_1L_chr = "ecdf")
depict(X1, x_vars_chr = "k6_total", what_1L_chr = "histogram")
depict(X1, x_vars_chr = "k6_total", x_labels_chr = "K6", y_labels_chr = "", as_percent_1L_lgl = T, what_1L_chr = "histogram", bins = 10)
depict(X1, x_vars_chr = "k6_total", x_labels_chr = NA_character_, z_vars_chr = "d_sex_birth_s", z_labels_chr = NA_character_, as_percent_1L_lgl = T, drop_missing_1L_lgl = T, position_xx = "dodge", what_1L_chr = "histogram", bins=10)
depict(X1, x_vars_chr = "k6_total", x_labels_chr = "K6", y_labels_chr = "", z_vars_chr = "d_sex_birth_s", z_labels_chr = "Sex", as_percent_1L_lgl = F, drop_missing_1L_lgl = T, what_1L_chr = "histogram", add = "mean", add_density = TRUE)
depict(X1, x_vars_chr = "k6_total", what_1L_chr = "qqplot")
depict(X1, x_vars_chr = "k6_total", y_labels_chr = "K6", drop_missing_1L_lgl = T, z_vars_chr = "d_studying_working", z_labels_chr = "", what_1L_chr = "qqplot")
depict(X, x_vars_chr = "round", y_vars_chr = "k6_total", what_1L_chr = "boxplot")
depict(X, x_vars_chr = "round", x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = "K6", z_labels_chr = "", drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T, what_1L_chr = "boxplot")
depict(X, x_vars_chr = "round", x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = NA_character_, z_labels_chr = "", drop_missing_1L_lgl = T, drop_ticks_1L_lgl = T, what_1L_chr = "boxplot", fill = "round", fill_single_1L_lgl = F)
depict(X, x_vars_chr = "round", x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = NA_character_, drop_missing_1L_lgl = T, z_vars_chr = "d_sex_birth_s", z_labels_chr = NA_character_, what_1L_chr = "boxplot", fill = "d_sex_birth_s", fill_single_1L_lgl = F)
depict(X, x_vars_chr = "round", x_labels_chr = "", y_vars_chr = "k6_total", y_labels_chr = "K6", drop_missing_1L_lgl = T, z_vars_chr = "d_sex_birth_s", z_labels_chr = NA_character_, what_1L_chr = "boxplot", add = "jitter")
depict(X1, x_vars_chr = "d_studying_working", what_1L_chr = "donutchart")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "", drop_missing_1L_lgl = T, line_1L_chr = "white", what_1L_chr = "donutchart")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, line_1L_chr = "white", what_1L_chr = "donutchart", lab.pos = "in", lab.font = "white")
depict(X8, x_vars_chr = "Count", z_vars_chr = "d_studying_working", z_labels_chr = "", as_percent_1L_lgl = T, line_1L_chr = "white", what_1L_chr = "donutchart", lab.pos = "in", lab.font = "white")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = NA_character_, drop_legend_1L_lgl = T, drop_missing_1L_lgl = T, line_1L_chr = "white", what_1L_chr = "donutchart", label = "d_studying_working")
depict(X2, x_vars_chr = "d_studying_working", y_vars_chr = "k6_total", what_1L_chr = "dotchart")
depict(X2, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_vars_chr = "k6_total", y_labels_chr = "K6", drop_missing_1L_lgl = T, fill_single_1L_lgl = T, recode_lup_r3 = y, what_1L_chr = "dotchart", add = "segment", add.params = list(color = "lightgray", size = 1.5), rotate = T, size = 3)
depict(X3, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT", y_vars_chr = "k6_total", y_labels_chr = "K6", z_vars_chr = "d_sex_birth_s", z_labels_chr = "SEX", drop_missing_1L_lgl = T, position_xx = ggplot2::position_dodge(0.3), recode_lup_r3 = x, what_1L_chr = "dotchart", add = "segment", add.params = list(color = "lightgray", size = 1.5), rotate = T, size = 3)
depict(X1, x_vars_chr = "d_studying_working", y_vars_chr = "k6_total", what_1L_chr = "dotplot")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT" , y_vars_chr = "k6_total", y_labels_chr = "K6", drop_legend_1L_lgl = T, drop_missing_1L_lgl = T, recode_lup_r3 = x, what_1L_chr = "dotplot", order = c("EDUCATION", "EMPLOYMENT","BOTH", "NEITHER"), size = 0.3)
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "EDUCATION AND EMPLOYMENT" ,y_vars_chr = "k6_total", y_labels_chr = "K6", z_vars_chr = "d_sex_birth_s", z_labels_chr = "SEX", drop_missing_1L_lgl = T, what_1L_chr = "dotplot", size = 0.35)
depict(X1, x_vars_chr = "d_studying_working", y_vars_chr = "k6_total", what_1L_chr = "errorplot")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = "K6", z_vars_chr = "d_sex_birth_s", z_labels_chr = NA_character_, drop_missing_1L_lgl = T, what_1L_chr = "errorplot", palette = "Paired", error.plot = "pointrange", position = ggplot2::position_dodge(0.5))
depict(X5, x_vars_chr = "round", y_vars_chr = "k6_total", what_1L_chr = "line")
depict(X, x_vars_chr = "round", x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = "K6", z_vars_chr = "d_sex_birth_s", z_labels_chr = NA_character_, drop_missing_1L_lgl = T, position_xx = ggplot2::position_dodge(0.5), what_1L_chr = "line", add = "mean_se")
depict(X7, x_vars_chr = "round", y_vars_chr = "k6_total", what_1L_chr = "paired")
depict(X7, x_vars_chr = "round", x_labels_chr = "", y_vars_chr = "k6_total", y_labels_chr = "K6", z_labels_chr = NA_character_, drop_ticks_1L_lgl = T, line_1L_chr = "lightgray", what_1L_chr = "paired")
depict(X1, x_vars_chr = "d_studying_working", what_1L_chr = "pie")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, line_1L_chr = "white", what_1L_chr = "pie", lab.pos = "in", lab.font = "white")
depict(X8, x_vars_chr = "Count", z_vars_chr = "d_studying_working", z_labels_chr = "", as_percent_1L_lgl = T, drop_missing_1L_lgl = T, line_1L_chr = "white", what_1L_chr = "pie", lab.pos = "in", lab.font = "white")
depict(X1, x_vars_chr = "d_studying_working", y_vars_chr = "k6_total", what_1L_chr = "strip")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = "K6", drop_legend_1L_lgl = T, drop_missing_1L_lgl = T, what_1L_chr = "strip", add = "mean_sd")
depict(X1, x_vars_chr = "d_studying_working", y_vars_chr = "k6_total", what_1L_chr = "violin")
depict(X1, x_vars_chr = "d_studying_working", x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = "", drop_legend_1L_lgl = T, drop_missing_1L_lgl = T, what_1L_chr = "violin", add = "jitter")
depict(X1, x_vars_chr = "phq9_total", y_vars_chr = "k6_total", what_1L_chr = "scatter")
depict(X1, x_vars_chr = "phq9_total", x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = "K6", drop_missing_1L_lgl = T, what_1L_chr = "scatter", add = "reg.line", size = 0.5, conf.int = TRUE, cor.coef = TRUE, cor.coeff.args = list(method = "pearson", label.x = 3, label.sep = "\n"))
depict(X1, x_vars_chr = "phq9_total", y_vars_chr = "k6_total", what_1L_chr = "scatterhist")
depict(X, x_vars_chr = "phq9_total", x_labels_chr = NA_character_, y_vars_chr = "k6_total", y_labels_chr = "K6", z_vars_chr = "round", drop_missing_1L_lgl = T, what_1L_chr = "scatterhist")
depict(X9, x_vars_chr = "d_studying_working", y_vars_chr = "d_sex_birth_s", z_vars_chr = "n", what_1L_chr = "balloonplot")
depict(X9, x_vars_chr = "d_studying_working", y_vars_chr = "d_sex_birth_s", z_vars_chr = "n", what_1L_chr = "balloonplot", fill_single_1L_lgl = T)
depict(X9, x_vars_chr = "d_studying_working", y_vars_chr = "d_sex_birth_s", z_vars_chr = "n", what_1L_chr = "balloonplot", size = 10, show.label=T)
depict(X9, x_vars_chr = "d_studying_working", y_vars_chr = "d_sex_birth_s", z_vars_chr = "n", style_1L_chr = "C", type_1L_chr = "viridis", what_1L_chr = "balloonplot", size = 10, show.label=T)
depict(X9, x_vars_chr = "d_studying_working", y_vars_chr = "d_sex_birth_s", z_vars_chr = "n", what_1L_chr = "balloonplot", palette = c("blue", "red"), size = 10, show.label=T)
depict(X10, x_vars_chr = "d_studying_working", y_vars_chr = "d_sex_birth_s", z_vars_chr = "n", what_1L_chr = "balloonplot", facet.by = c("c_p_diag_s", "c_clinical_staging_s"), ggtheme = ggplot2::theme_bw(), palette = c("blue", "red"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.