ggtreeExtra icon indicating copy to clipboard operation
ggtreeExtra copied to clipboard

Missing negative values after adjust pwidth parameter

Open Microbion opened this issue 2 years ago • 9 comments

I adjust the pwidth to control the width of the barplot layer, as #2 told. But negative values will be missing when pwidth is higher.

ggtree(tree, layout = "radial", ladderize = FALSE, branch.length = "none", aes(color = group)) +
xlim(-10, NA) +
scale_color_viridis_d() +
ggnewscale::new_scale_color() +
geom_fruit(data = deseq_res, geom = geom_bar, stat = "identity", orientation = "y", pwidth = 0.25, offset = 0.2,
           mapping = aes(x = log2FoldChange, y = row, color = color))

And it's going well, like this: image But if I ajust pwidth to 5 (or smaller than 5), I get Warning message "“Removed 355 rows containing missing values (position_stackx).”", and lose lots of negative values. image Is it suite for my situation?

Microbion avatar Sep 08 '22 15:09 Microbion

How about re-installing the github version by remotes::install_github('YuLab-SMU/ggtreeExtra').

> library(ggtreeExtra)
ggtreeExtra v1.7.0.990 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
> set.seed(1024)
> tr <- rtree(100)
> dd = data.frame(id=tr$tip.label, value=rnorm(100))
> dd %<>% dplyr::mutate(group=dplyr::case_when(value < 0 ~ "A", TRUE ~ 'B'))
> p <- ggtree(tr, layout="circular")
> p + geom_fruit(data = dd, geom=geom_col, mapping=aes(x=value, fill=group, y=id), offset=.2, pwidth=.2, axis.params=list(axis='x', text.size=2, vjust=1), grid.params=list())

xx

PS: I think you can use fill=color instead of color = color in aes of geom_fruit, then you remove the ggnewscale::new_scale_color()

xiangpin avatar Sep 09 '22 08:09 xiangpin

Thanks for your reply @xiangpin. My checked package version, it's 1.7.0.998 now. And tested your code, everything went ok. But when I tried my data, I got the same warning again. I guess it may be caused by my value's distribution. The absolute values of negative values are higher than positive values, and not the random distribution. Here's the density plot of values which I used in bar plot. But I have no idea about why it comes. image

How about re-installing the github version by remotes::install_github('YuLab-SMU/ggtreeExtra').

> library(ggtreeExtra)
ggtreeExtra v1.7.0.990 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
> set.seed(1024)
> tr <- rtree(100)
> dd = data.frame(id=tr$tip.label, value=rnorm(100))
> dd %<>% dplyr::mutate(group=dplyr::case_when(value < 0 ~ "A", TRUE ~ 'B'))
> p <- ggtree(tr, layout="circular")
> p + geom_fruit(data = dd, geom=geom_col, mapping=aes(x=value, fill=group, y=id), offset=.2, pwidth=.2, axis.params=list(axis='x', text.size=2, vjust=1), grid.params=list())

xx

PS: I think you can use fill=color instead of color = color in aes of geom_fruit, then you remove the ggnewscale::new_scale_color()

Microbion avatar Sep 09 '22 10:09 Microbion

Sorry, I can not debug, because I can not reproduce the issue even though I built the same dataset you told. Would you mind sharing your data via email if you do not want to make it public?

xiangpin avatar Sep 09 '22 12:09 xiangpin

I reproduced the issue with your example with adding xlim(-2, NA) and rising pwidth to 0.5. I guess that xlim function acts on the whole layer and geom_fruit seems not treat it well, or do you have any advise like ggnew_scale package? Here's my code below:

set.seed(1024)
tr <- rtree(100)
dd = data.frame(id=tr$tip.label, value=rnorm(100)) %>%
mutate(group=dplyr::case_when(value < 0 ~ "A", TRUE ~ 'B'), 
)
p <- ggtree(tr, layout="circular") + xlim(-2, NA)
p +
geom_fruit(data = dd, geom = geom_col, mapping=aes(x=value, fill=group, y=id), offset = .2, pwidth=.5)

And the warning showed again image PS: Sorry for forgetting festivals in the last post. Wish you and your family a happy Mid-Autumn Festival and Mr. Yu a happy Teacher's Day too.

Microbion avatar Sep 13 '22 02:09 Microbion

So is it the same question? #17

Microbion avatar Sep 13 '22 02:09 Microbion

Yes, This issue was caused by the xlim . You can use geom_blank(aes(x=-2)) to replace xlim.

> library(dplyr)
> library(ggplot2)
> library(ggtreeExtra)
> library(ggtree)
> set.seed(1024)
> tr <- rtree(100)
> dd = data.frame(id=tr$tip.label, value=rnorm(100)) %>% mutate(group=dplyr::case_when(value < 0 ~ "A", TRUE ~ 'B'))
> p <- ggtree(tr, layout="circular")
> p + geom_blank(aes(x=-2)) + geom_fruit(data = dd, geom = geom_col, mapping=aes(x=value, fill=group, y=id), offset = .2, pwidth=.5)

xiangpin avatar Sep 14 '22 11:09 xiangpin

Yes, that's what my want indeed. Thanks very much.

Yes, This issue was caused by the xlim . You can use geom_blank(aes(x=-2)) to replace xlim.

> library(dplyr)
> library(ggplot2)
> library(ggtreeExtra)
> library(ggtree)
> set.seed(1024)
> tr <- rtree(100)
> dd = data.frame(id=tr$tip.label, value=rnorm(100)) %>% mutate(group=dplyr::case_when(value < 0 ~ "A", TRUE ~ 'B'))
> p <- ggtree(tr, layout="circular")
> p + geom_blank(aes(x=-2)) + geom_fruit(data = dd, geom = geom_col, mapping=aes(x=value, fill=group, y=id), offset = .2, pwidth=.5)

Microbion avatar Sep 14 '22 14:09 Microbion

@xiangpin I am wondering whether the xlim_tree is compatible with this case?

GuangchuangYu avatar Sep 15 '22 15:09 GuangchuangYu

Yes, the xlim_tree can also solve the problem.

> library(dplyr)
> library(ggplot2)
> library(ggtreeExtra)
> library(ggtree)
> set.seed(1024)
> tr <- rtree(100)
> dd = data.frame(id=tr$tip.label, value=rnorm(100)) %>% mutate(group=dplyr::case_when(value < 0 ~ "A", TRUE ~ 'B'))
> p <- ggtree(tr, layout="circular")
> p + geom_fruit(data = dd, geom = geom_col, mapping=aes(x=value, fill=group, y=id), offset = .2, pwidth=.5) + xlim_tree(-10)

xx

xiangpin avatar Sep 16 '22 07:09 xiangpin