-
Notifications
You must be signed in to change notification settings - Fork 177
Description
Dear Guangchuang and ggtree repo helpers,
I am having trouble making a figure that includes a tree and add a number of continuous and discrete traits both with my own data and with this example from your book (7.3.1 Visualize a tree with multiple associated matrices; https://yulab-smu.top/treedata-book/chapter7.html), which yields a good reproducible example.
At run time, with code pasted below in its entirety, I get the following error after error:
> p2 <- p1 + new_scale_fill()
Error: <ggtree> object properties are invalid:
- @mapping must be <ggplot2::mapping>, not S3<data.frame>
If I execute that code chunk and add a call to p1, which I think is the cause of the problem, I get:
> p1
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 (25).
✖ Fix the following mappings: `from`, `to`, and `.panel`.
Run `rlang::last_trace()` to see where the error occurred.
I may be missing something trivial, but it's a frustrating problem that I can't seem to solve for my own data plots.
I appreciate your time and help solving it very much!
P
P.S. Here is the example code from 7.3.1 and session info:
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
circ <- ggtree(tree, layout = "circular")
df <- data.frame(first=c("a", "b", "a", "c", "d", "d", "a",
"b", "e", "e", "f", "c", "f"),
second= c("z", "z", "z", "z", "y", "y",
"y", "y", "x", "x", "x", "a", "a"))
rownames(df) <- tree$tip.label
df2 <- as.data.frame(matrix(rnorm(39), ncol=3))
rownames(df2) <- tree$tip.label
colnames(df2) <- LETTERS[1:3]
p1 <- gheatmap(circ, df, offset=.8, width=.2,
colnames_angle=95, colnames_offset_y = .25) +
scale_fill_viridis_d(option="D", name="discrete\nvalue")
library(ggnewscale)
p2 <- p1 + new_scale_fill()
gheatmap(p2, df2, offset=15, width=.3,
colnames_angle=90, colnames_offset_y = .25) +
scale_fill_viridis_c(option="A", name="continuous\nvalue")
(This yields the above-described error(s).)
> sessionInfo()
R version 4.5.0 (2025-04-11)
Platform: aarch64-apple-darwin20
Running under: macOS Sequoia 15.5
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
Random number generation:
RNG: Mersenne-Twister
Normal: Inversion
Sample: Rounding
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: America/Chicago
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggnewscale_0.5.2 ggtree_3.16.3 ggplot2_4.0.0
loaded via a namespace (and not attached):
[1] jsonlite_2.0.0 gtable_0.3.6 crayon_1.5.3 dplyr_1.1.4
[5] compiler_4.5.0 tidyselect_1.2.1 Rcpp_1.1.0 aplot_0.2.9
[9] gridGraphics_0.5-1 parallel_4.5.0 ggplotify_0.1.3 tidyr_1.3.1
[13] ggfun_0.2.0 scales_1.4.0 lattice_0.22-7 R6_2.6.1
[17] patchwork_1.3.2 generics_0.1.4 tidytree_0.4.6 yulab.utils_0.2.1
[21] tibble_3.3.0 pillar_1.11.1 RColorBrewer_1.1-3 treeio_1.32.0
[25] rlang_1.1.6 fs_1.6.6 S7_0.2.0 lazyeval_0.2.2
[29] cli_3.6.5 withr_3.0.2 magrittr_2.0.4 digest_0.6.37
[33] grid_4.5.0 rappdirs_0.3.3 lifecycle_1.0.4 nlme_3.1-168
[37] vctrs_0.6.5 glue_1.8.0 farver_2.1.2 ape_5.8-1
[41] purrr_1.1.0 tools_4.5.0 pkgconfig_2.0.3