Programming

Resolving ggplot2 and ggfortify Deprecation Warnings in R

Learn how to resolve ggplot2 and ggfortify deprecation warnings for residual plots in R. Updated syntax for aes_string(), fortify(), and size aesthetics.

6 answers 1 view

How can I resolve deprecation warnings when using ggplot2 and ggfortify packages for residual plots in R? My code is generating warnings about fortify(), aes_string(), and size aesthetic deprecations.

When working with ggplot2 and ggfortify for residual plots in R, you’ll encounter deprecation warnings about fortify(), aes_string(), and the size aesthetic as these packages have evolved to newer versions. To resolve these issues, you need to update your code to use aes_() instead of aes_string(), ensure compatible package versions, and modify how size aesthetics are specified in your plots.


Contents


Understanding ggplot2 and ggfortify Deprecation Warnings

The ggplot2 package has undergone significant evolution, with version 4.0.0 introducing several deprecations to improve consistency and future-proof the package. These changes affect how we create visualizations, particularly when working with statistical plots like residual plots through ggfortify.

ggfortify, a package that extends ggplot2 functionality for statistical analysis visualization, has also updated its approach to maintain compatibility with newer ggplot2 versions. The deprecation warnings you’re seeing are intentional signals from these packages that certain functions and syntax approaches are being phased out in favor of newer, more robust alternatives.

What’s happening is that packages like ggplot2 and ggfortify are standardizing their APIs to make them more consistent, maintainable, and extensible. While your code might still work with these warnings, they indicate that you’re using features that may be removed in future versions.


Resolving aes_string() Deprecation in ggplot2

The most common deprecation warning you’ll encounter is related to aes_string(). This function has been deprecated in favor of aes_(), which provides better support for programmatic generation of plots while maintaining the same functionality.

Here’s how to transition from aes_string() to aes_():

Old code (deprecated):

r
library(ggplot2)
plot <- ggplot(data, aes_string(x = "predictor", y = "response")) + 
 geom_point()

New code (recommended):

r
library(ggplot2)
plot <- ggplot(data, aes_(x = ~predictor, y = ~response)) + 
 geom_point()

The key difference is that aes_() uses formulas with the tilde (~) operator to specify column names, rather than character strings. This approach is more flexible and integrates better with R’s evaluation model.

Why did they make this change? According to the official ggplot2 documentation, this change was made to improve consistency with other ggplot2 functions and to provide better support for programmatic generation of plots. The tilde syntax allows for more flexible variable handling and better evaluation in different contexts.

For dynamic variable names, you can still achieve the same functionality with aes_():

r
x_var <- "predictor"
y_var <- "response"
plot <- ggplot(data, aes_(x = as.name(x_var), y = as.name(y_var))) + 
 geom_point()

This approach resolves the deprecation warning while maintaining the same dynamic mapping capabilities.


Updating fortify() Function Usage in ggfortify

When working with residual plots, you might be using the fortify() function from ggfortify to prepare statistical model objects for plotting. This function has also seen updates in recent versions.

ggfortify versions 0.4.18 and 0.4.19 specifically addressed compatibility issues with ggplot2 4.0.0. If you’re experiencing issues with fortify(), updating to the latest version of ggfortify should resolve many of these problems.

Here’s how to properly use fortify() with residual plots:

r
library(ggfortify)
library(ggplot2)

# Fit a model
model <- lm(y ~ x1 + x2, data = my_data)

# Use fortify() to prepare for plotting
fortified_model <- fortify(model)

# Create residual plot
ggplot(fortified_model, aes(.fitted, .resid)) +
 geom_point() +
 geom_hline(yintercept = 0, linetype = "dashed")

The fortify() function creates a data frame with columns like .fitted (predicted values) and .resid (residuals) that ggplot2 can use directly. This approach remains valid, but ensure you’re using a recent version of ggfortify (0.4.18 or later) for full compatibility.

If you encounter specific issues with fortify(), check the ggfortify GitHub repository for updates or alternative approaches. The package maintainers have been actively working to maintain compatibility with ggplot2 changes.


Fixing size Aesthetic Deprecation Issues

Another common deprecation warning relates to how the size aesthetic is specified in ggplot2. In recent versions, ggplot2 has standardized how size parameters are handled across different geoms and scales.

Old code that may generate warnings:

r
ggplot(data, aes(x, y)) +
 geom_point(size = 3)

New recommended approach:

r
ggplot(data, aes(x, y)) +
 geom_point(aes(size = 3))

The key change is moving the size parameter inside the aes() function when it’s meant to be mapped to a variable. If you want a fixed size that’s not mapped to data, the recommended approach is:

r
ggplot(data, aes(x, y)) +
 geom_point()

Then specify size as a geom parameter, not an aesthetic:

r
ggplot(data, aes(x, y)) +
 geom_point(size = 3)

This distinction becomes clearer when you’re working with plots where size should represent a variable versus when it’s just a fixed visual parameter:

Size mapped to a variable:

r
ggplot(data, aes(x, y, size = z)) +
 geom_point() +
 scale_size(range = c(1, 10))

Fixed size:

r
ggplot(data, aes(x, y)) +
 geom_point(size = 3)

This change helps clarify the difference between aesthetics that represent data variables and parameters that control visual properties.


Best Practices for Modern ggplot2 and ggfortify Implementation

To avoid deprecation warnings and future-proof your code, consider these best practices:

  1. Keep packages updated: Always use the latest stable versions of ggplot2 and ggfortify. You can check for updates with:
r
# Check for ggplot2 updates
update.packages("ggplot2")

# Check for ggfortify updates
update.packages("ggfortify")
  1. Use the modern aes_() syntax: Replace all instances of aes_string() with aes_() in your code.

  2. Be mindful of aesthetic specifications: Clearly distinguish between aesthetics mapped to variables and fixed visual parameters.

  3. Check package NEWS files: Before updating packages, check their NEWS files (like the ggfortify NEWS.md) to understand what changes might affect your code.

  4. Adopt consistent coding patterns: Standardize how you specify aesthetics and parameters across your codebase to minimize future changes needed.

  5. Use version control: Keep your code in a version control system like Git so you can track changes and revert if needed when updating packages.

By following these practices, you’ll minimize deprecation warnings and make it easier to adapt to future changes in these popular R visualization packages.


Complete Example: Updated Residual Plot Code

Here’s a complete example showing how to create a residual plot with all deprecation warnings resolved:

r
# Load required packages
library(ggplot2)
library(ggfortify)

# Create example data
set.seed(123)
x <- rnorm(100)
y <- 2 + 3*x + rnorm(100)
data <- data.frame(x = x, y = y)

# Fit a linear model
model <- lm(y ~ x, data = data)

# Use fortify() to prepare model data for plotting
fortified_model <- fortify(model)

# Create residual plot with updated syntax
residual_plot <- ggplot(fortified_model, aes_(x = ~.fitted, y = ~.resid)) +
 geom_point(alpha = 0.7) + # Fixed alpha parameter, not aesthetic
 geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
 labs(title = "Residual Plot",
 x = "Fitted Values",
 y = "Residuals") +
 theme_minimal()

# Display the plot
print(residual_plot)

# Add a Q-Q plot for normality check
qq_plot <- autoplot(model, which = 2, color = "blue") +
 theme_minimal()

# Display the Q-Q plot
print(qq_plot)

This example demonstrates:

  1. Using aes_() instead of aes_string() for dynamic variable mapping
  2. Properly specifying the fortify() function from ggfortify
  3. Correctly handling the size aesthetic (not used in this example, but showing proper parameter specification)
  4. Using autoplot() from ggfortify, which has been updated to work with modern ggplot2 syntax

The code should now run without deprecation warnings while producing the same visualizations as before.


Sources

  1. ggplot2 Documentation — Official reference for ggplot2 package and its functions: https://ggplot2.tidyverse.org/
  2. ggfortify CRAN Package Info — Package documentation and version information: https://cran.r-project.org/web/packages/ggfortify/index.html
  3. ggfortify GitHub Repository — Source code and development updates: https://github.com/sinhrks/ggfortify
  4. ggfortify NEWS.md — Version history and deprecation notes: https://github.com/sinhrks/ggfortify/blob/master/NEWS.md
  5. aes_string() Reference — Documentation for the deprecated aes_string function: https://ggplot2.tidyverse.org/reference/aes_string.html

Conclusion

Resolving deprecation warnings in ggplot2 and ggfortify requires updating your code to use modern syntax and ensuring compatible package versions. By transitioning from deprecated functions like aes_string() to their replacements, and understanding the changes to aesthetics like size, you can maintain your visualizations while future-proofing your code. Staying current with package updates and documentation will help prevent similar issues in the future.

H

ggplot2 is a system for declaratively creating graphics based on “The Grammar of Graphics.” The package has evolved significantly over the years, with version 4.0.0 introducing several deprecations. Key changes include the replacement of aes_string() with aes_() for dynamic aesthetic mapping, and updates to how size aesthetics are handled. The package maintains backward compatibility where possible but encourages users to adopt newer syntax for better performance and future compatibility.

CRAN / Documentation Portal

ggfortify provides unified plotting tools for statistical analysis results, including residual plots. The package version 0.4.19 is compatible with ggplot2 4.0.0 and addresses several deprecation warnings. It requires ggplot2 version 2.0.0 or higher and offers autoplot functions to visualize statistical results from popular R packages using ggplot2 syntax.

Yuan Tang / Senior Principal Engineer

The ggfortify package offers fortify and autoplot functions to enable ggplot2 visualization of statistical results. Recent versions (0.4.18 and 0.4.19) have fixed compatibility issues with ggplot2 4.0.0. The package has been maintained by Yuan Tang since 2015 and provides a single plotting interface for various statistical analyses including GLM, time series, PCA, clustering, and survival analysis.

Yuan Tang / Senior Principal Engineer

The NEWS.md file documents that ggfortify version 0.4.18 and 0.4.19 fixed failed tests with ggplot2 4.0.0. Version 0.4.16 resolved warnings in R CMD check related to S3 generic/method consistency. The package switched to MIT license in version 0.4.14 and temporarily dropped support for xts and timeSeries objects in version 0.4.2. These updates address the deprecation warnings users encounter when using ggfortify with newer ggplot2 versions.

H

The aes_string() function has been deprecated in ggplot2 and now redirects to aes_(). This change was made to improve consistency with other ggplot2 functions and to provide better support for programmatic generation of plots. Users should transition from aes_string() to aes_() for dynamic aesthetic mapping in their ggplot2 code.

Authors
H
Chief Scientist
Yuan Tang / Senior Principal Engineer
Senior Principal Engineer
M
Package Author
Sources
ggplot2 / Documentation Portal
Documentation Portal
CRAN / Documentation Portal
Documentation Portal
GitHub / Developer Tools
Developer Tools
Verified by moderation
NeuroAnswers
Moderation
Resolving ggplot2 and ggfortify Deprecation Warnings in R