Overview
vitalBayes provides publication-ready visualization functions for all model types, with built-in support for colorblind-safe palettes and multilingual labels.
Color Palettes
library(vitalBayes)
# List available palettes
list_vital_palettes()
# Default retro-wave palette
vital_colors(5, "default")
# Two-sex palette (pink/cyan)
vital_colors(2, "sex")
# Colorblind-safe alternatives
vital_colors(5, "okabe_ito")
vital_colors(2, "sex_cb") # Orange/blue for sex
vital_colors(5, "viridis")
vital_colors(5, "tol_bright")
# Check if a palette is colorblind-safe
is_colorblind_safe("okabe_ito") # TRUE
is_colorblind_safe("sex") # FALSEThe vitalBayes Theme
All plots use theme_vital(), a clean ggplot2 theme
optimized for publications:
library(ggplot2)
# Use standalone
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
theme_vital()
# Customize
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
theme_vital(base_size = 14) +
theme(legend.position = "bottom")plot_birth_ogive()
Visualize birth size transition probability:
library(data.table)
# Load simulated data
data(growth_data)
# Fit birth model
birth_fit <- fit_bayesian_birth(
embryo_lts = growth_data[embryo == TRUE, fl],
free_swimming_lts = growth_data[embryo == FALSE, fl]
)
# Basic birth ogive plot
plot_birth_ogive(
fit = birth_fit,
embryo_lengths = growth_data[embryo == TRUE, fl],
freeswim_lengths = growth_data[embryo == FALSE, fl]
)
# Customized
plot_birth_ogive(
fit = birth_fit,
embryo_lengths = growth_data[embryo == TRUE, fl],
freeswim_lengths = growth_data[embryo == FALSE, fl],
show_data = TRUE,
show_b50_line = TRUE,
colors = vital_colors(1, "sunset"),
x_lab = "Fork Length (cm)",
title = "Birth Size Transition"
)plot_maturity_ogive()
Visualize maturity probability curves:
# Prepare maturity data
mat_data <- growth_data[embryo == FALSE & !is.na(mat)]
# Fit two-sex maturity model
L50_fit <- fit_bayesian_maturity(
maturity = "mat",
lt = "fl",
sex = "sex",
data = mat_data,
use_pooling = TRUE
)
# Basic maturity ogive plot
plot_maturity_ogive(
fit = L50_fit,
type = "length",
data = mat_data,
x_col = "fl",
maturity_col = "mat",
sex_col = "sex"
)Maturity Ogive Arguments
plot_maturity_ogive(
fit = L50_fit,
type = "length", # or "age"
data = mat_data,
# Data columns
x_col = "fl",
maturity_col = "mat",
sex_col = "sex",
# Display options
show_data = TRUE, # Overlay observed points
show_rug = TRUE, # Rug plot at top/bottom
show_x50_line = TRUE, # Vertical line at x50
# Appearance
colors = NULL, # Auto from vital_colors("sex")
colorblind = FALSE, # Use colorblind-safe palette
facet_sex = TRUE, # Separate panels by sex
# Labels
x_lab = "Fork Length (cm)",
y_lab = "Probability of Maturity",
title = "Length-at-Maturity Ogive"
)Multilingual Labels
# French
plot_maturity_ogive(
fit = L50_fit,
type = "length",
data = mat_data,
sex_labels = c("female" = "Femelle", "male" = "Mâle"),
x_lab = "Longueur à la fourche (cm)",
y_lab = "Probabilité de maturité"
)
# Spanish
plot_maturity_ogive(
fit = L50_fit,
type = "length",
data = mat_data,
sex_labels = c("female" = "Hembra", "male" = "Macho"),
x_lab = "Longitud furcal (cm)",
y_lab = "Probabilidad de madurez"
)
# Japanese
plot_maturity_ogive(
fit = L50_fit,
type = "length",
data = mat_data,
sex_labels = c("female" = "メス", "male" = "オス"),
x_lab = "尾叉長 (cm)",
y_lab = "成熟確率"
)plot_growth_curve()
Visualize fitted growth curves with uncertainty:
# Prepare growth data
gdata <- growth_data[embryo == FALSE & !is.na(age)]
# Fit growth model
growth_fit <- fit_bayesian_growth(
lt = "fl",
age = "age",
sex = "sex",
data = gdata,
model = "vb",
k_based = FALSE,
L50_fit = L50_fit
)
# Basic growth curve plot
plot_growth_curve(
fit = growth_fit,
data = gdata,
age_col = "age",
length_col = "fl",
sex_col = "sex"
)Growth Curve Arguments
plot_growth_curve(
fit = growth_fit,
data = gdata,
# Data columns
age_col = "age",
length_col = "fl",
sex_col = "sex",
# Prediction range
age_range = c(0, 50), # Override auto-detection
n_points = 100, # Points for smooth curve
# Display options
show_data = TRUE, # Overlay observed points
show_50_ci = TRUE, # Show 50% credible interval
facet_sex = TRUE, # Separate panels by sex
# Appearance
data_alpha = 0.4,
ribbon_alpha = 0.2,
ribbon_alpha_50 = 0.3,
line_width = 1.2,
colors = NULL, # Auto from vital_colors("sex")
colorblind = FALSE,
# Model specification (must match fitting call)
k_based = FALSE,
which_model = 1, # 1=VB, 2=Gompertz, 3=Logistic
# Labels
x_lab = "Age (years)",
y_lab = "Fork Length (cm)",
title = NULL
)Additional Layers
Add custom ggplot2 layers:
library(ggplot2)
plot_growth_curve(
fit = growth_fit,
data = gdata,
additional_layers = list(
# Add horizontal line at L50
geom_hline(yintercept = 75, linetype = "dashed", color = "gray50"),
# Add annotation
annotate("text", x = 5, y = 77, label = "L50", size = 3)
),
theme_args = list(
legend.position = "bottom",
plot.title = element_text(hjust = 0.5)
)
)compare_growth_models()
Side-by-side comparison of multiple growth models:
# Fit multiple models
mat_aged <- growth_data[embryo == FALSE & !is.na(mat) & !is.na(age)]
t50_fit <- fit_bayesian_maturity(
maturity = "mat", age = "age", sex = "sex",
data = mat_aged
)
vb_fit <- fit_bayesian_growth(
lt = "fl", age = "age", sex = "sex", data = gdata,
model = "vb", k_based = FALSE, L50_fit = L50_fit, t50_fit = t50_fit
)
gomp_fit <- fit_bayesian_growth(
lt = "fl", age = "age", sex = "sex", data = gdata,
model = "gompertz", k_based = FALSE, L50_fit = L50_fit, t50_fit = t50_fit
)
logis_fit <- fit_bayesian_growth(
lt = "fl", age = "age", sex = "sex", data = gdata,
model = "logistic", k_based = FALSE, L50_fit = L50_fit, t50_fit = t50_fit
)
# Compare visually
compare_growth_models(
"von Bertalanffy" = vb_fit,
"Gompertz" = gomp_fit,
"Logistic" = logis_fit,
data = gdata,
age_col = "age",
length_col = "fl",
sex_col = "sex",
k_based_list = c(FALSE, FALSE, FALSE),
which_model_list = c(1, 2, 3)
)
# Colorblind-safe
compare_growth_models(
"von Bertalanffy" = vb_fit,
"Gompertz" = gomp_fit,
data = gdata,
colorblind = TRUE
)plot_residuals()
Diagnostic residual plots for growth models:
# All diagnostic plots
plot_residuals(
fit = growth_fit,
data = gdata,
age_col = "age",
length_col = "fl",
sex_col = "sex",
type = "all" # Combined panel
)
# Individual plot types
plot_residuals(fit = growth_fit, data = gdata, type = "fitted")
plot_residuals(fit = growth_fit, data = gdata, type = "qq")
plot_residuals(fit = growth_fit, data = gdata, type = "histogram")
plot_residuals(fit = growth_fit, data = gdata, type = "age")Saving Publication Figures
library(patchwork)
# Create multi-panel figure
p1 <- plot_maturity_ogive(L50_fit, type = "length", data = mat_data)
p2 <- plot_growth_curve(growth_fit, data = gdata)
combined <- p1 / p2 + plot_annotation(tag_levels = "A")
# Save at publication resolution
ggsave("Figure1.pdf", combined, width = 10, height = 12)
ggsave("Figure1.png", combined, width = 10, height = 12, dpi = 300)Using Different Datasets for Demonstration
The package includes multiple datasets for different visualization needs:
# Balanced data (growth_data) - best for general demonstrations
data(growth_data)
# Imbalanced data - shows pooling effects on uncertainty bands
data(imbalanced_data)
mat_imbal <- imbalanced_data[embryo == FALSE & !is.na(mat)]
L50_imbal <- fit_bayesian_maturity(
maturity = "mat", lt = "fl", sex = "sex",
data = mat_imbal, use_pooling = TRUE
)
# Note the wider bands for males (sparse sex)
plot_maturity_ogive(
fit = L50_imbal,
type = "length",
data = mat_imbal,
title = "Maturity Ogive with Imbalanced Sex Ratio (150F, 34M)"
)See Also
- Statistical Methods Guide — Mathematical background for all models
-
vital_colors(),theme_vital()— Styling utilities -
list_vital_palettes(),is_colorblind_safe()— Palette information -
vignette("fit_bayesian_maturity")— Maturity model fitting -
vignette("fit_bayesian_growth")— Growth model fitting
This document is part of the vitalBayes R package. For bug reports, feature requests, or questions, please visit the GitHub repository.
