Beyond the Basics

Advanced Plots with ggplot2

Creating compelling and publication-ready visualizations is a critical skill in data analysis and storytelling. While ggplot2 provides a robust set of tools for creating basic plots, its true power lies in its ability to allow for intricate customizations. In this post, we will delve deeper into ggplot2 and explore advanced techniques to:

  1. Use custom functions to generate plots
  2. Create publication-ready plots with intricate customizations

By the end of this post, you’ll have a solid understanding of how to take your visualizations to the next level, creating customized, visually appealing plots that are ready for publication.


1. Using Custom Functions to Generate Plots

Sometimes, you might need to generate multiple similar plots with slight variations (e.g., for different subsets of data or changing only a few parameters). Creating custom functions in R can streamline this process, making your code more efficient, reusable, and clean.

Step 1: Define a Custom Plotting Function

You can define a custom function to encapsulate the plotting logic for ggplot2. This allows you to pass in different datasets or parameters as arguments. Here’s a simple example of a function that generates a scatter plot with a regression line for any given dataset:

r
Copy code
library(ggplot2)

# Custom function for scatter plots
create_scatter_plot <- function(data, x_var, y_var, color_var = NULL) {
  plot <- ggplot(data, aes_string(x = x_var, y = y_var, color = color_var)) +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    theme_minimal() +
    labs(x = x_var, y = y_var, color = color_var)

  return(plot)
}

In this function:

  • x_var and y_var: Variables for the x- and y-axis are passed as string arguments.
  • color_var: An optional variable for color aesthetics.
  • geom_smooth: Adds a linear regression line to the scatter plot.

Step 2: Using the Custom Function

Once the function is defined, you can reuse it for different datasets and variable combinations. For example, to create a scatter plot with mtcars:

r
Copy code
create_scatter_plot(mtcars, "wt", "mpg", "cyl")

This will create a scatter plot with wt on the x-axis, mpg on the y-axis, and colors representing the cyl (number of cylinders) variable.

Step 3: Adding More Customizations to the Function

To make the function even more flexible, you can add additional parameters for other customization options, such as titles, themes, or labels:

r
Copy code
create_scatter_plot <- function(data, x_var, y_var, color_var = NULL, title = NULL) {
  plot <- ggplot(data, aes_string(x = x_var, y = y_var, color = color_var)) +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    theme_minimal() +
    labs(x = x_var, y = y_var, color = color_var, title = title)

  return(plot)
}

This way, you can add a title dynamically when calling the function:

r
Copy code
create_scatter_plot(mtcars, "wt", "mpg", "cyl", "Weight vs. Miles Per Gallon")

2. Creating Publication-Ready Plots with Intricate Customizations

When creating plots for publication, whether it’s for research papers, reports, or presentations, you need more control over aesthetics, layout, and the overall design. ggplot2 offers a wide range of customizations, from controlling colors and themes to adjusting text sizes and plot elements. Let’s explore some advanced techniques for creating polished, publication-quality plots.

Step 1: Customizing Themes and Layouts

The default ggplot2 theme is minimal, but you can enhance it with additional styling, like adjusting the background, grid lines, and legend positioning.

Example: Customizing Theme

r
Copy code
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(title = "Weight vs. Miles Per Gallon",
       x = "Weight", y = "Miles Per Gallon", color = "Cylinders") +
  theme_minimal(base_size = 14) +   # Change base font size
  theme(
    panel.background = element_rect(fill = "lightgray", color = "black"),  # Panel background
    plot.title = element_text(hjust = 0.5, size = 18, face = "bold"),      # Title customization
    legend.position = "top",                                               # Position legend on top
    axis.text.x = element_text(angle = 45, hjust = 1)                       # Rotate x-axis labels
  )

In this example:

  • theme_minimal(base_size = 14): Sets the base font size for the plot.
  • panel.background: Changes the background of the plot panel.
  • element_text(): Customizes text properties (e.g., title alignment, size, and style).
  • legend.position: Moves the legend to the top of the plot.
  • axis.text.x: Rotates the x-axis labels for better readability.

Step 2: Using Custom Color Palettes

Colors play a crucial role in making plots visually appealing. ggplot2 provides several ways to customize color schemes, such as using color scales for continuous and categorical variables.

Example: Custom Color Scales

r
Copy code
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  scale_color_manual(values = c("red", "green", "blue")) +  # Custom colors for 'cyl' levels
  labs(title = "Weight vs. Miles Per Gallon",
       x = "Weight", y = "Miles Per Gallon", color = "Cylinders") +
  theme_minimal()
  • scale_color_manual(): This function lets you define a custom color palette for the categorical variable (cyl).
  • values: A vector specifying the color for each category in cyl (e.g., red for 4 cylinders, green for 6, blue for 8).

You can also use scale_fill_manual() for filling color properties in bar plots or histograms.

Step 3: Fine-Tuning Text Elements

For high-quality plots, you’ll want to ensure that the text in your plots is clear and legible, especially for publications. This includes controlling text size, font family, and styling.

Example: Customizing Text Elements

r
Copy code
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(size = 3) +
  labs(title = "Weight vs. Miles Per Gallon",
       x = "Weight", y = "Miles Per Gallon") +
  theme(
    plot.title = element_text(size = 18, face = "bold", family = "Arial"),
    axis.title.x = element_text(size = 14, family = "Times New Roman"),
    axis.title.y = element_text(size = 14, family = "Times New Roman"),
    axis.text = element_text(size = 12)
  )
  • element_text(size, face, family): Controls the size, style (bold, italic), and font family of text elements (title, axis labels, etc.).
  • axis.title.x and axis.title.y: Customize the appearance of axis labels.

Step 4: Adding Annotations for Clarity

Annotations can help highlight specific points or trends in the data, making your plot more informative.

Example: Adding Annotations

r
Copy code
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(aes(color = factor(cyl)), size = 3) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(title = "Weight vs. Miles Per Gallon",
       x = "Weight", y = "Miles Per Gallon", color = "Cylinders") +
  theme_minimal() +
  annotate("text", x = 3.5, y = 30, label = "High MPG", size = 5, color = "blue")  # Adding annotation
  • annotate(): Adds text annotations to specific coordinates on the plot.
  • x and y: Define the position for the annotation.

Conclusion

Creating advanced plots in ggplot2 involves a deep understanding of customization options, from defining custom plotting functions to refining aesthetics for publication-quality results. By learning how to manipulate themes, colors, fonts, and annotations, you can create visually stunning, informative visualizations.

In this post, we’ve:

  • Explored the power of custom functions to generate reusable plots.
  • Learned how to create publication-ready plots with intricate customizations such as themes, colors, and text styling.