YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/contribution-tree-data/
839 stars 172 forks source link

Error: Cannot create zero-length unit vector ("unit" subsetting) #315

Open decenwang opened 4 years ago

decenwang commented 4 years ago

Hi Dr. Yu,

When I re-run the "10.1.1 facet_widths" https://yulab-smu.github.io/treedata-book/chapter10.html#facet-utilities command line is "facet_widths(p2, widths = c(1, 2))"', which leads to the error. I tried to search the solution with "Cannot create zero-length unit vector ("unit" subsetting)". it seems that it is a error by inconformity of unit. Could you help it? My R version is 4.0.0. Thanks a lot!

Decen

xiayh17 commented 4 years ago

Hi,

You can modify the facet_widths function by running the following command before you run the facet_widths(p2, widths = c(1, 2))

facet_widths <- function(p, widths) { if (!is.null(names(widths))) {

if (is.ggtree(p) && !is.null(names(widths))) {

## .panel <- levels(p$data$.panel)
.panel <- panel_col_levels(p)
w <- rep(1, length=length(.panel))
names(w) <- .panel
w[names(widths)] <- widths
widths <- w

} gt <- ggplot_gtable(ggplot_build(p)) for(i in seq_along(widths)) { j <- gt$layout$l[grep(paste0('panel-1-', i), gt$layout$name)] gt$widths[j] = widths[i] * gt$widths[j] } return(ggplotify::as.ggplot(gt)) }

install.packages("ggplotify") library(ggplotify)