bio-data-visualization-ggplot2-fundamentals
Version Compatibility
Reference examples tested with: ggplot2 3.5+, scales 1.3+, ggrepel 0.9.5+, ggtext 0.1.2+, viridis 0.6+, scico 1.5+, patchwork 1.2+ (axes='collect' requires 1.2.0+).
Before using code patterns, verify installed versions match. If versions differ:
- R:
packageVersion('<pkg>')then?function_nameto verify parameters
If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
ggplot2 Fundamentals
"Build a publication figure in R" -> Express the figure as data + aesthetic mappings + one or more geometries + scales + facets + theme. The grammar of graphics (Wilkinson 2005; Wickham 2010 J Comput Graph Stat 19:3) makes each visual element separately addressable — change scales without rewriting geoms; swap geom_point for geom_violin without touching aesthetics.
- R:
ggplot(data, aes(x, y)) + geom_point() + scale_color_manual(...) + theme_classic() - Programmatic:
aes(x = .data[[var]])for tidy-eval;!!sym(var)for older base R style
The Three Modern Defaults
- theme_classic() + remove panel grid + Okabe-Ito palette as the publication baseline.
theme_minimaladds light gridlines;theme_bwadds a panel border; both work buttheme_classicis the cleanest for journals.