Lesson · 40 min · Free
Publication-Quality Plots with the Tidyverse
Publication-Quality Plots with the Tidyverse Computational Biomedicine: From Command Line to Single-Cell Publication-Quality Plots with the Tidyverse In the world of biomedical research, the ability to effectively commun
Computational Biomedicine: From Command Line to Single-Cell
Publication-Quality Plots with the Tidyverse
In the world of biomedical research, the ability to effectively communicate complex data is paramount. While statistical analysis provides the foundation for our conclusions, it is often through compelling visualizations that our findings truly resonate with an audience. This lesson will guide you through the process of creating publication-quality plots using the tidyverse package in R, specifically leveraging ggplot2 . We'll move beyond default settings to create figures that are not only accurate but also aesthetically pleasing and informative, suitable for journal submissions and scientific presentations. ggplot2 operates on a "grammar of graphics" principle, meaning you build plots layer by layer. This structured approach offers immense flexibility and control over every aspect of your visualization. Key components include data, aesthetic mappings ( aes() ), geometric objects ( geom_*() ), and statistical transformations ( stat_*() ). Beyond these core elements, we can fine-tune titles, labels, legends, colors, fonts, and themes to meet the rigorous standards of scientific publication. Before diving into the code, ensure you have the tidyverse package installed and loaded. If not, you can install it using install.packages("tidyverse") and load it with library(tidyverse) . For demonstration, we'll use a hypothetical dataset of drug efficacy measurements across different treatment groups.
Example 1: Basic Scatter Plot with Enhancements
Let's start with a simple scatter plot and gradually add layers to improve its publication quality. We'll simulate some data for this example. # Load the tidyverse package library(tidyverse) # Simulate some data for drug efficacy set.seed(123) # for reproducibility drug_data % mutate(Treatment = factor(Treatment, levels = c("Control", "Drug A", "Drug B"))) # Order factors # Basic scatter plot p1 In the enhanced example, we made several critical improvements: geom_jitter() : Replaces geom_point() to spread out overlapping points, revealing the distribution better. stat_summary() : Adds mean points and standard error bars, providing summary statistics directly on the plot. scale_color_manual() : Allows for precise control over color assignments, crucial for consistent branding or accessibility. labs() : Sets informative titles and axis labels. theme_minimal() and theme() : Customizes the overall appearance. We adjusted font sizes, styles, removed minor gridlines, and added axis lines to give it a professional look.
Example 2: Faceting and Statistical Annotations
Faceting allows you to display subsets of your data in separate panels, which is incredibly useful for comparing across different conditions or variables. We'll also demonstrate how to add statistical annotations. # Simulate more complex data for drug efficacy with a "Gender" factor drug_data_gender % mutate(Gender = sample(c("Male", "Female"), 100, replace = TRUE)) # Box plot faceted by Gender with custom theme and annotations p3 Here, we introduced: facet_wrap(~ Gender) : Creates separate plots for each gender, allowing for direct comparison. geom_boxplot(outlier.shape = NA) : Suppresses default outlier plotting from the boxplot, as we are showing individual points with jitter. theme_bw() : Provides another clean, black-and-white theme. legend.position = "bottom" : Adjusts legend placement for better layout. strip.background and strip.text : Customize the appearance of the facet labels. annotate() : Used to manually add text labels (like significance stars) to specific coordinates. For more complex statistical annotations, packages like ggsignif or ggpubr can be very helpful. ggsave() : Essential for saving your plots in high resolution, suitable for publication, in various formats like PNG or PDF.
Key Takeaways
ggplot2 builds plots layer by layer, offering granular control over every visual element. Always choose appropriate geoms (e.g., geom_jitter for discrete X and continuous Y) to best represent your data. Customization of colors, labels, titles, and themes is crucial for publication quality. Faceting ( facet_wrap , facet_grid ) is powerful for comparing subsets of data. Utilize ggsave() to export high-resolution figures in suitable formats (e.g., PDF for vector graphics, PNG for raster). Consider accessibility: use colorblind-friendly palettes (e.g., from RColorBrewer ) and ensure text is legible.
Practice Exercise
Using the mtcars dataset (built into R), create a scatter plot showing 'mpg' (miles per gallon) against 'wt' (weight), colored by 'cyl' (number of cylinders). Enhance this plot to be publication-ready by adding a linear regression line for each cylinder group ( geom_smooth(method = "lm", se = FALSE) ), customizing colors, adding a descriptive title and axis labels, and applying a clean theme. Save your final plot as a PDF file.
Watch the full lesson — free
This topic is part of Computational Biomedicine: From Command Line to Single-Cell, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →