ggtree
ggtree copied to clipboard
Error: Cannot create zero-length unit vector ("unit" subsetting)
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
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)