Lesson · 40 min · Free
Intro to R for Publication Plots
Intro to R for Publication Plots body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 15px; border-radius: 5px; overflow-x: auto; } code {
Intro to R for Publication Plots
Welcome to the "Intro to R for Publication Plots" lesson within our Deep Generative Models course! While this course primarily focuses on the theoretical and practical aspects of GANs, VAEs, and Diffusion models, effectively communicating your research findings is paramount. This lesson will equip you with foundational skills in R, a powerful statistical programming language, specifically tailored for generating high-quality, publication-ready plots. Even if you're working with complex deep learning models, visualizing their performance, latent space, or generated outputs often requires robust plotting tools. R, particularly with the ggplot2 package, offers unparalleled flexibility and aesthetic control, making it a favorite among researchers for creating impactful figures. For pharmacy and biotech students, the ability to clearly visualize dose-response curves, gene expression profiles, drug efficacy data, or the output of generative models (e.g., synthetic molecular structures, protein sequences) is crucial. This introduction will focus on getting you started with basic data manipulation and plotting, emphasizing principles that translate directly to creating compelling visuals for your research papers, presentations, and posters. We'll touch upon loading data, creating simple scatter plots and box plots, and customizing elements to meet publication standards.
Getting Started with R and ggplot2
Before we dive into plotting, you'll need to have R and RStudio installed. RStudio is an integrated development environment (IDE) that makes working with R much more user-friendly. Once installed, the first step in any R session is often to load necessary packages. For plotting, the undisputed champion is ggplot2 . If you haven't installed it yet, you can do so with install.packages("ggplot2") . After installation, you load it into your current session using library(ggplot2) . Let's consider a common scenario in biotech: comparing the expression levels of a gene (or the efficacy of a drug) across different treatment groups. We'll start by creating some synthetic data to simulate this. In R, data is often stored in data frames, which are similar to tables in a spreadsheet. # Install and load ggplot2 (run install.packages("ggplot2") once if not installed) # install.packages("ggplot2") library(ggplot2) # Create some synthetic data for demonstration set.seed(123) # for reproducibility # Simulate gene expression data for two treatment groups data_expression Now that we have our data, let's create a basic scatter plot to visualize the distribution of gene expression for each treatment group. ggplot2 uses a layered grammar of graphics, meaning you build plots by adding components like data, aesthetic mappings ( aes() ), geometric objects ( geom_point() , geom_boxplot() ), and labels. # Basic scatter plot of gene expression by treatment group ggplot(data_expression, aes(x = Treatment, y = Gene_Expression)) + geom_point() + labs(title = "Gene Expression Levels by Treatment Group", x = "Treatment Group", y = "Gene Expression (Arbitrary Units)") + theme_minimal() # A clean, minimalist theme This code generates a scatter plot showing individual data points. While useful, for comparing distributions, a box plot or violin plot is often more informative. Let's modify the previous example to create a box plot, which effectively summarizes the median, quartiles, and outliers for each group. # Box plot of gene expression by treatment group ggplot(data_expression, aes(x = Treatment, y = Gene_Expression, fill = Treatment)) + # 'fill' maps to treatment for different colors geom_boxplot() + geom_jitter(width = 0.2, alpha = 0.6) + # Add jittered points to show individual data points labs(title = "Gene Expression Distribution by Treatment Group", x = "Treatment Group", y = "Gene Expression (Arbitrary Units)") + theme_bw() + # Black and white theme, often good for publications theme(legend.position = "none") # Remove the legend if colors are redundant with x-axis labels In this box plot example, we added fill = Treatment inside aes() to color the boxes by treatment group, which enhances visual distinction. geom_jitter() adds a small amount of random noise to the position of each point, preventing overplotting and revealing the density of points within each group. The theme_bw() provides a clean, publication-friendly aesthetic, and theme(legend.position = "none") removes the redundant legend as the x-axis already labels the groups. These small adjustments are crucial for creating professional-looking figures.
Key Takeaways
R and ggplot2 are powerful tools for creating high-quality, publication-ready plots. Data in R is often stored in data frames. ggplot2 uses a "grammar of graphics" approach: data → aesthetics → geoms → facets → stats → coordinates → themes. aes() maps variables from your data to visual properties (e.g., x-axis, y-axis, color, fill). geom_point() creates scatter plots, geom_boxplot() creates box plots, and geom_jitter() helps visualize individual data points in dense plots. Customizing labels ( labs() ) and themes ( theme_minimal() , theme_bw() ) is essential for publication quality.
Practice Exercise
Imagine you've conducted an experiment comparing the efficacy of three different drug formulations (A, B, C) on reducing a biomarker level. Create a new data frame with synthetic data for three groups. Each group should have 30 observations for a 'Biomarker_Level'. Formulations A and B should have similar biomarker levels (e.g., mean 50, sd 5), while Formulation C should show a significantly lower biomarker level (e.g., mean 30, sd 4). Your task is to: Create a data frame named drug_efficacy_data with two columns: Formulation and Biomarker_Level . Generate a box plot (similar to the last example) comparing the Biomarker_Level across the three Formulation groups. Ensure your plot includes appropriate axis labels, a clear title, and uses a publication-friendly theme (e.g., theme_bw() ). Add jittered points to your box plot to show individual data points.
Watch the full lesson — free
This topic is part of Deep Generative Models: GANs, VAEs & Diffusion, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →