Skip to content

gheatmap() fails on ggplot2 4.0 because attr(p, "mapping") becomes a global aesthetic #686

@xiehs211

Description

@xiehs211

Summary

gheatmap() attaches the heatmap column metadata via attr(p, "mapping") <- mapping. ggplot2 4.0 treats this as a
global aesthetic, so stat_tree() receives the 3-row heatmap mapping and errors with “Fix the following mappings:
from, to, .panel”.

Environment

  • R 4.5.1
  • ggtree 3.16.3
  • ggplot2 4.0.1
  • ape 5.8-1

Minimal Reproduction

library(ggtree)
library(ape)

tree <- rtree(5)
mat <- matrix(runif(10), nrow = 5,
dimnames = list(tree$tip.label, c("A", "B")))

p <- ggtree(tree)
gheatmap(p, mat)

Error in `stat_tree()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the
  data (9).
✖ Fix the following mappings: `from`, `to`, and `.panel`.
Run `rlang::last_trace()` to see where the error occurred.

Analysis / Workaround

Removing/renaming that attribute restores plotting:
p2 <- gheatmap(p, mat)
attr(p2, "mapping") <- NULL
print(p2)

Image

Suggested Fix

Rename the attribute (e.g. attr(p, "heatmap_mapping")) or drop it so ggplot2 no longer confuses it with the global
mapping slot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions