給定一棵樹,帶有由一組著色的提示標簽,例如:
library(ggplot2)
library(ggtree)
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
meta = data.frame(label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"),
group = c(rep("mammal", 5), rep("insect", 5), rep("bird", 3)))
p = ggtree(tree) %< % meta
geom_tiplab(aes(color = group))
圖例將包含a而不是所需的正方形。ggplot2檔案說使用“override.aes”來覆寫這個行為:
p guides(color = guide_legend(override.aes = list(size = 4,
label = "",
shape = 15)))
這不起作用。覆寫作業,我可以改變大小,顏色,洗掉a但最重要的是我不能使用不同的形狀(在這種情況下是正方形)。
較新版本的 ggplot2 有該key_glyph選項,但這在使用geom_tiplab.
另一個奇怪的行為是,在使用時geom_tipppoint,覆寫圖例有效。值得注意的是,上述策略以前可以洗掉a,但是在最新的 R/ggplot2/ggtree 中,上述策略不起作用。這與最新版本ggtree而不是舊版本有關。
關于如何覆寫geom_tiplab()圖例形狀的任何建議?
我的環境:
R version 4.1.2 (2021-11-01)
ggplot2 version 3.3.5
ggtree version 3.2.1
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 20

uj5u.com熱心網友回復:
我不確定是否有一種聰明的方法可以做到這一點,但一個潛在的解決方法是將標簽更改為 unicode 'square' 字符,例如
library(tidyverse)
library(ggtree)
#> ggtree v3.0.4 For help: https://yulab-smu.top/treedata-book/
#>
#> If you use ggtree in published research, please cite the most appropriate paper(s):
#>
#> 1. Guangchuang Yu. Using ggtree to visualize data on tree-like structures. Current Protocols in Bioinformatics, 2020, 69:e96. doi:10.1002/cpbi.96
#> 2. Guangchuang Yu, Tommy Tsan-Yuk Lam, Huachen Zhu, Yi Guan. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution 2018, 35(12):3041-3043. doi:10.1093/molbev/msy194
#> 3. Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution 2017, 8(1):28-36. doi:10.1111/2041-210X.12628
#>
#> Attaching package: 'ggtree'
#> The following object is masked from 'package:tidyr':
#>
#> expand
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
meta = data.frame(label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"),
group = c(rep("mammal", 5), rep("insect", 5), rep("bird", 3)))
p = ggtree(tree) %< % meta
geom_tiplab(aes(color = group))
guides(color = guide_legend(override.aes = list(label = "\u25A1", size = 6)))
p

由
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376442.html
