ggtreeExtra icon indicating copy to clipboard operation
ggtreeExtra copied to clipboard

Trouble showing subset of data

Open pgcudahy opened this issue 3 years ago • 3 comments

I've got a phylogeny that I'd like to annotate but am having issues. The annotation data is in the form of

image

This code works:

p <- ggtree(Taiwan_MKansasii_raxml_ng_outgroup_rooted, layout="circular")

p <- rotate_tree(p, 90)

p + geom_fruit(data=annotate_df,
                geom=geom_tile,
                mapping=aes(y=tip_label, x=name, fill=value),
                  color = "grey50", offset = 0.04,size = 0.02,
    axis.params=list(axis="x",
                     text.angle = 0,
                     text.size = 4,
                     line.size = 0,
                     hjust = -.2,
                     title = "",
                     title.height = 0.1,
                     title.size = 4))

which produces image

But I think the image is too busy and only want to focus on the value of "WGY", so I changed the data argument to annotate_df %>% filter(name == "WGY")

p <- ggtree(Taiwan_MKansasii_raxml_ng_outgroup_rooted, layout="circular")

p <- rotate_tree(p, 90)

p + geom_fruit(data=annotate_df %>% filter(name  ==  "WGY"),
                geom=geom_tile,
                mapping=aes(y=tip_label, x=name, fill=value),
                  color = "grey50", offset = 0.04,size = 0.02,
    axis.params=list(axis="x",
                     text.angle = 0,
                     text.size = 4,
                     line.size = 0,
                     hjust = -.2,
                     title = "",
                     title.height = 0.1,
                     title.size = 4))

But that throws a bunch of errors

Warning message in max(abs(dat[[paste0("new_", xid)]]), na.rm = TRUE):
“no non-missing arguments to max; returning -Inf”
Warning message in min(x):
“no non-missing arguments to min; returning Inf”
Warning message in max(x):
“no non-missing arguments to max; returning -Inf”
Warning message in min(diff(sort(x))):
“no non-missing arguments to min; returning Inf”
Warning message:
“Removed 217 rows containing missing values (geom_tile).”
Warning message:
“Removed 4 rows containing missing values (geom_segment).”
Warning message:
“Removed 1 rows containing missing values (geom_text).”
Warning message:
“Removed 1 rows containing missing values (geom_text).”

How can I just get one circle of tiles showing the value of "WGY" and ignore the other 4 values of the "name" column in my dataframe?

pgcudahy avatar Sep 13 '22 08:09 pgcudahy

Please re-install ggtreeExtra by remotes::install_github('YuLab-SMU/ggtreeExtra'), the issue was fixed by the github version.

PS. if you want the adjust the width of geom_tile when only one unique x value (using width argument belonging to geom_tile), you can refer to the answer.

xiangpin avatar Sep 14 '22 12:09 xiangpin

Thanks very much for your reply, but I still have the same issue even after installing the github version

When I try the format used in your link to another issue I get

p <- ggtree(Taiwan_MKansasii_raxml_ng_outgroup_rooted, layout="circular")

p <- rotate_tree(p, 90)

p_annotated <- outgroup_tree2 %<+% annotate_df %>% filter(name == "WGY")

p_annotated +
    geom_fruit(geom = geom_tile,
    mapping = aes(fill = value))

image

But no matter what I choose for pwidth or offset the tree is always covered by the tiles

pgcudahy avatar Sep 15 '22 13:09 pgcudahy

I am sorry, I forgot to push the update to the github. Please re-install it now. You can use width argument to adjust the width of tile layer when value of x mapped in aes is only one unique value.

> library(ggtreeExtra)
ggtreeExtra v1.7.0.991 For help:
https://yulab-smu.top/treedata-book/

If you use the ggtree package suite in published research, please cite
the appropriate paper(s):

S Xu, Z Dai, P Guo, X Fu, S Liu, L Zhou, W Tang, T Feng, M Chen, L
Zhan, T Wu, E Hu, Y Jiang, X Bo, G Yu. ggtreeExtra: Compact
visualization of richly annotated phylogenetic data. Molecular Biology
and Evolution. 2021, 38(9):4039-4042. doi: 10.1093/molbev/msab166
> library(ggtree)
ggtree v3.5.3 For help: https://yulab-smu.top/treedata-book/

If you use the ggtree package suite in published research, please cite
the appropriate paper(s):

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

S Xu, Z Dai, P Guo, X Fu, S Liu, L Zhou, W Tang, T Feng, M Chen, L
Zhan, T Wu, E Hu, Y Jiang, X Bo, G Yu. ggtreeExtra: Compact
visualization of richly annotated phylogenetic data. Molecular Biology
and Evolution. 2021, 38(9):4039-4042. doi: 10.1093/molbev/msab166

G Yu. Data Integration, Manipulation and Visualization of Phylogenetic
Trees (1st ed.). Chapman and Hall/CRC. 2022. ISBN: 9781032233574
> set.seed(123)
> tr <- rtree(60)
> dat <- data.frame(id=tr$tip.label, group=rep(c('A','B','C','D','E'), each = 60), value=rep(c(0, 1), time=60*5))
> dat2 <- dat[sample(nrow(dat), 200),]
> p <- ggtree(tr, layout='circular')
> p <- rotate_tree(p, 90)
Coordinate system already present. Adding new coordinate system, which will replace the existing one.
> library(ggplot2)
> p1 <- p + geom_fruit(data = dat2, geom = geom_tile, mapping=aes(x=group, y=id, fill=as.factor(value)), color='grey90', axis.params=list(axis='x', text.size=3))
> p2 <- p + geom_fruit(data = dat2 %>% dplyr::filter(group=='A'), geom = geom_tile, mapping=aes(x=group, y=id, fill=as.factor(value)), color='grey90', axis.params=list(axis='x', text.size=3), width=.3)
> p1 + p2

xx

xiangpin avatar Sep 16 '22 06:09 xiangpin