wilkox / treemapify

🌳 Draw treemaps in ggplot2
http://wilkox.org/treemapify
214 stars 18 forks source link

Is there a way to make squarified layouts do rows first then columns? #43

Open slyrus opened 3 years ago

slyrus commented 3 years ago

I almost have the layout I'm looking for, with 4 groups in my treemap, but no matter which corner I start from, I always get the x-axis split into two equally, but not the y-axis.

e.g.:

  local({
      start <- "topleft"
      layout <- "squarified"
      tibble::tibble(A = c(1,1,0,0),
                     B = c(0,1,1,0),
                     pct = c(0.69, 0.14, 0.06, 0.11)) %>%
          dplyr::mutate(label = paste("A=", A, "\n", "B=", B, sep="")) %>%
          ggplot2::ggplot(ggplot2::aes(area = pct,
                                       subgroup = A,
                                       subgroup2 = B,
                                       label = label)) +
          treemapify::geom_treemap(start = start, layout = layout) +
          treemapify::geom_treemap_text(start = start)
  })

What I'm looking for is the A=1,B=0 and A=1,B=1 groups placed along the top in boxes of equal height. I can get equal width, but not equal height and starting from different corners or reversing the subgroups doesn't seem to help.

wilkox commented 3 years ago

Unfortunately there's no way to force a layout like this with the current set of layout algorithms. The squarified, srow and scol layouts are all variants on an algorithm that prioritises drawing tiles with an aspect ratio as close to one as possible, without any regard to how many tiles or subgroups end up in a row or column. I suppose I could add an algorithm that gives each subgroup its own row or column, and then draws the tiles within that subgroup as single row or column; I'm curious to know what the use case would be for this though?

slyrus commented 3 years ago

It's not a major need, I just, conceptually, like the look of a solid row on top, rather than a solid column on the left. 4 regions may be something of a degenerate case and I can imagine this might be difficult to support in general, but the results from the existing algorithm are almost exactly what i want, I just want it rotated 90 degrees. coord_flip doesn't work as a workaround. Anyway, great package. Thanks!