m-jahn / WeightedTreemaps

Create Voronoi and Sunburst Treemaps from Hierarchical data
GNU General Public License v3.0
47 stars 9 forks source link

View data Calculated under voronoiTreemap #31

Closed msgoussi closed 1 year ago

msgoussi commented 1 year ago

data(mtcars) mtcars$car_name = gsub(" ", "\n", row.names(mtcars))

generate treemap; set seed to obtain same pattern every time

tm <- voronoiTreemap( data = mtcars, levels = c("gear", "car_name"), cell_size = "wt", shape = "rounded_rect", seed = 123 )

Is it possible for user to access the data calculated under voronoiTreemap, ie tm$data will show dataframe with three column grea, car_name and cell_size (either a sum of (wt) or frequency if wt is not pased to cell_size)

Also, i am wondering how to view tm in RStudio or access tm list.

m-jahn commented 1 year ago

Currently it's not possible to access the data from treemap generation (area etc) simply as data frame. The data is however available in the cells slot of the tm object. What you can do as a workaround is simply to iterate over the cells slot, which is just a list-type object, and collect the results in a df:

library(WeightedTreemaps)
library(dplyr)

# load example data
data(mtcars)
mtcars$car_name = gsub(" ", "\n", row.names(mtcars))

# generate treemap
tm <- voronoiTreemap(
  data = mtcars,
  levels = c("gear", "car_name"),
  cell_size = "wt",
  shape = "rounded_rect"
)

lapply(tm@cells, function(x) {
  x <- x[c("level", "name", "target", "weight", "area")]
  as.data.frame(x)
}) %>%
  bind_rows()

This will give you the folling result:

   level                 name     target        weight       area
1      1                    3 0.56714780  104.80259013 2177764.68
2      1                    4 0.30499650  -36.62366330 1207780.28
3      1                    5 0.12785570 -361.56348634  535976.92
4      2         AMC\nJavelin 0.05882957  -21.76786992  140433.38
5      2  Cadillac\nFleetwood 0.08991420   27.69589860  174590.74
6      2          Camaro\nZ28 0.06576581   -3.44765726  147405.34
...

In the end I might include a summary() function that will do exactly that.

m-jahn commented 1 year ago

Added a commit 5b19ac5e that provides new functions/generics to extract data from results objects (as.data.frame, summary, get_polygons).

Can be closed @msgoussi ?