## ----setup, include = FALSE--------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.2, out.width = "100%" ) library(ggkodom) library(ggplot2) ## ----data--------------------------------------------------------------------- set.seed(42) n_subjects <- 25 n_obs_per <- sample(6:12, n_subjects, replace = TRUE) df <- do.call(rbind, lapply(seq_len(n_subjects), function(i) { n <- n_obs_per[i] base <- rnorm(1, mean = 7.5, sd = 1.2) trend <- rnorm(1, mean = -0.02, sd = 0.01) time <- sort(runif(n, 0, 24)) value <- base + trend * time + rnorm(n, sd = 0.4) data.frame( subject_id = sprintf("P%03d", i), visit_month = time, relative_month = time - min(time), hba1c = pmax(4, value), arm = ifelse(i <= 12, "Treatment", "Control"), age = sample(30:75, 1), stringsAsFactors = FALSE ) })) ## ----basic-------------------------------------------------------------------- ggplot(df, aes(x = visit_month, id = subject_id, colour = hba1c)) + geom_kodom_line() + scale_colour_kodom() + labs(x = "Visit (months)", y = "", colour = "HbA1c (%)") + theme_kodom() ## ----sort, fig.height = 5----------------------------------------------------- # Top lanes = highest mean HbA1c, bottom = lowest ggplot(df, aes(x = visit_month, id = subject_id, colour = hba1c)) + geom_kodom_line(sort_by = "mean") + scale_colour_kodom() + labs(x = "Visit (months)", y = "", title = 'sort_by = "mean"') + theme_kodom(legend_position = "top") ## ----transform---------------------------------------------------------------- ggplot(df, aes(x = relative_month, id = subject_id, colour = hba1c)) + geom_kodom_line(sort_by = "first") + scale_colour_kodom() + labs(x = "Visit (months)", y = "", title = 'sort_by = "mean"') + theme_kodom(legend_position = "top") ## ----n_max, fig.height = 6---------------------------------------------------- ggplot(df, aes(x = visit_month, id = subject_id, colour = hba1c)) + geom_kodom_line(sort_by = "mean", n_max = 12) + scale_colour_kodom() + labs(x = "Visit (months)", y = "", title = "12 randomly sampled subjects") + theme_kodom() ## ----points-compare, fig.height = 6------------------------------------------- p_base <- ggplot(df, aes(x = visit_month, id = subject_id, colour = hba1c)) + scale_colour_kodom() + labs(x = "Visit (months)", y = "") + theme_kodom() p_base + geom_kodom_line(show_points = TRUE) + labs(title = "show_points = TRUE (default)") ## ----points-off, fig.height = 6----------------------------------------------- p_base + geom_kodom_line(show_points = FALSE) + labs(title = "show_points = FALSE") ## ----points-na---------------------------------------------------------------- p_base + geom_kodom_line(shape = NA) + labs(title = "shape = NA — same effect as show_points = FALSE") ## ----point-shapes, fig.height = 6--------------------------------------------- # shape 21 = filled circle — fill controls interior, colour controls border ggplot( df, aes(x = visit_month, id = subject_id, colour = hba1c, fill = hba1c) ) + geom_kodom_line(shape = 21, size = 2.5, stroke = 0.6) + scale_colour_kodom() + scale_fill_kodom() + labs(x = "Visit (months)", y = "", title = "shape = 21 with fill + colour") + theme_kodom() ## ----point-diamonds, fig.height = 6------------------------------------------- # shape 23 = filled diamond ggplot( df, aes(x = visit_month, id = subject_id, colour = hba1c, fill = hba1c) ) + geom_kodom_line(shape = 23, size = 2.5) + scale_colour_kodom() + scale_fill_kodom() + labs(x = "Visit (months)", y = "", title = "shape = 23 (diamond)") + theme_kodom() ## ----linewidth, fig.height = 6------------------------------------------------ ggplot(df, aes(x = visit_month, id = subject_id, colour = hba1c)) + geom_kodom_line(linewidth = 3, alpha = 0.5) + scale_colour_kodom() + labs(x = "Visit (months)", y = "", title = "linewidth = 3, alpha = 0.5") + theme_kodom() ## ----discrete, fig.height = 6------------------------------------------------- ggplot(df, aes(x = visit_month, id = subject_id, colour = hba1c)) + geom_kodom_line(sort_by = "mean") + scale_colour_kodom( discretize = TRUE, color_breaks = c(5.7, 6.5, 8), name = "HbA1c (%)" ) + labs(x = "Visit (months)", y = "", title = "Discrete clinical bands") + theme_kodom() ## ----size-age----------------------------------------------------------------- ggplot(df, aes( x = visit_month, id = subject_id, colour = hba1c, size = age )) + geom_kodom_line(sort_by = "mean", linewidth = 0.4) + scale_colour_kodom() + scale_size_continuous(range = c(1, 4), name = "Age") + labs( x = "Visit (months)", y = "", title = "Point size = patient age, path width fixed" ) + theme_kodom() ## ----alpha-age---------------------------------------------------------------- ggplot(df, aes( x = visit_month, id = subject_id, colour = hba1c, alpha = age )) + geom_kodom_line(sort_by = "mean") + scale_colour_kodom() + scale_alpha_continuous(range = c(0.2, 1), name = "Age") + labs( x = "Visit (months)", y = "", title = "Opacity = patient age" ) + theme_kodom() ## ----size-alpha-age----------------------------------------------------------- ggplot(df, aes( x = visit_month, id = subject_id, colour = hba1c, size = age, alpha = age )) + geom_kodom_line(sort_by = "mean", linewidth = 0.3) + scale_colour_kodom() + scale_size_continuous(range = c(1, 4), name = "Age") + scale_alpha_continuous(range = c(0.25, 1), guide = "none") + labs( x = "Visit (months)", y = "", title = "Size + opacity both encode age" ) + theme_kodom() ## ----facet, fig.height = 6---------------------------------------------------- ggplot(df, aes(x = visit_month, id = subject_id, colour = hba1c)) + geom_kodom_line(sort_by = "mean") + scale_colour_kodom(color_breaks = c(5.7, 6.5, 8)) + facet_wrap(~arm) + labs(x = "Visit (months)", y = "", title = "Treatment vs. Control") + theme_kodom() ## ----y-labels, fig.height = 6------------------------------------------------- # Suppress y labels for large cohorts — usually the right choice ggplot(df, aes(x = visit_month, id = subject_id, colour = hba1c)) + geom_kodom_line(sort_by = "mean") + scale_colour_kodom() + labs(x = "Visit (months)", y = "") + theme_kodom() + theme(axis.text.y = element_blank()) ## ----palette, fig.height = 4-------------------------------------------------- # View the three anchor colors and any interpolated expansion scales::show_col(kodom_colors(7))