Fix annotation_custom() error with ggplot2 4.0.0+ in pathway error bar plots#187
Draft
Copilot wants to merge 5 commits into
Draft
Fix annotation_custom() error with ggplot2 4.0.0+ in pathway error bar plots#187Copilot wants to merge 5 commits into
Copilot wants to merge 5 commits into
Conversation
…meric Co-authored-by: cafferychen777 <82987086+cafferychen777@users.noreply.github.com>
…patibility Co-authored-by: cafferychen777 <82987086+cafferychen777@users.noreply.github.com>
Co-authored-by: cafferychen777 <82987086+cafferychen777@users.noreply.github.com>
Co-authored-by: cafferychen777 <82987086+cafferychen777@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix annotation_custom error in patchwork plot for GGPICRUSt2
Fix annotation_custom() error with ggplot2 4.0.0+ in pathway error bar plots
Oct 16, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Users encountered a critical error when generating pathway error bar plots with ggplot2 4.0.0+ and R 4.4.0+:
This error occurred specifically when:
ko_to_kegg = TRUEto convert KO to KEGG pathwaysorder = "pathway_class"to group by pathway classp_values_bar = TRUEfor log2 fold change visualizationThe error prevented plots from rendering, making the package unusable for users with these settings on recent R/ggplot2 versions.
Root Cause
The
pathway_errorbar()function usedannotation_custom(grid::rectGrob(...))to add colored background rectangles for pathway class annotations. ggplot2 4.0.0 introduced changes to the grob rendering pipeline that exposed compatibility issues:grid::rectGrob()contain internal unit-based positioningcoord_cartesian(clip = "off")and patchwork composition, coordinate transformations attempted to rescale these unit objectsscales::rescale()function has no method for unit objects, causing the errorSolution
Replaced
annotation_custom(grid::rectGrob(...))withggplot2::annotate("rect", ...):Before:
After:
This approach:
Additional Improvements
Added defensive
as.numeric()conversions to ensure all coordinate values are plain numeric without attributes:Testing
Added comprehensive regression test that:
ko_to_kegg = TRUEwith pathway class annotations works correctlyp_value_bar = TRUE(4-panel) andFALSE(3-panel) layoutsImpact
✅ No breaking changes - API remains identical
✅ No visual changes - Output looks exactly the same
✅ Backward compatible - Works with ggplot2 3.x and 4.0.0+
✅ Better maintainability - Cleaner code using native ggplot2 features
✅ Future-proof - Less likely to break with future ggplot2 updates
Users can now successfully use ggpicrust2 with ggplot2 4.0.0+ and R 4.4.0+ without encountering the unit rescaling error.
Fixes #184
Original prompt
Fixes #186
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.