From be5fd7470942d6cb023d4f7cad229908ce194d6b Mon Sep 17 00:00:00 2001 From: jahn Date: Mon, 8 Jan 2024 08:07:44 +0100 Subject: [PATCH] reduced building time --- vignettes/WeightedTreemaps.R | 164 ++++++++++++++++---------------- vignettes/WeightedTreemaps.Rmd | 22 +++-- vignettes/WeightedTreemaps.html | 17 ++-- 3 files changed, 105 insertions(+), 98 deletions(-) diff --git a/vignettes/WeightedTreemaps.R b/vignettes/WeightedTreemaps.R index 3d40e5e..3a6fd7a 100644 --- a/vignettes/WeightedTreemaps.R +++ b/vignettes/WeightedTreemaps.R @@ -15,74 +15,74 @@ library(WeightedTreemaps) 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 -) - -## ----fig.width = 5, fig.height = 5, out.width = "50%", fig.align = 'center'---- -drawTreemap(tm, label_size = 2.5, label_color = "white") - -## ----fig.width = 9, fig.height = 9, out.width = "100%", fig.align = 'center', warning = FALSE---- -drawTreemap(tm, title = "treemap 1", label_size = 2, - color_type = "categorical", color_level = 1, - layout = c(2, 2), position = c(1, 1), legend = TRUE) - -drawTreemap(tm, title = "treemap 2", label_size = 2, - color_type = "categorical", color_level = 2, border_size = 3, - add = TRUE, layout = c(2, 2), position = c(1, 2), legend = TRUE) - -drawTreemap(tm, title = "treemap 3", label_size = 2, - color_type = "both", color_level = 1, - add = TRUE, layout = c(2, 2), position = c(2, 1), legend = TRUE) - -drawTreemap(tm, title = "treemap 4", label_size = 2, - color_type = "cell_size", color_level = 2, - color_palette = heat.colors(10), - border_color = grey(0.4), label_color = grey(0.4), - add = TRUE, layout = c(2, 2), position = c(2, 2), - title_color = "black", legend = TRUE) - -## ----message = FALSE, error = FALSE, results = 'hide'------------------------- -# set seed to obtain same df every time -set.seed(123) -df <- data.frame(A = sample(10:100, 45)) - -tm1 <- voronoiTreemap( - data = df, levels = "A", - cell_size = "A", - shape = "rounded_rect", - positioning = "random" -) - -tm2 <- voronoiTreemap( - data = df, levels = "A", - cell_size = "A", - shape = "rounded_rect", - positioning = "regular" -) - -tm3 <- voronoiTreemap( - data = df, levels = "A", - cell_size = "A", - shape = "rounded_rect", - positioning = "clustered" -) - -## ----fig.width = 12, fig.height = 4, out.width = "100%", fig.align = 'center', warning = FALSE---- -drawTreemap(tm1, title = "positioning = 'random'", border_size = 3, - layout = c(1,3), position = c(1, 1)) - -drawTreemap(tm2, title = "positioning = 'regular'", border_size = 3, - add = TRUE, layout = c(1,3), position = c(1, 2)) - -drawTreemap(tm3, title = "positioning = 'clustered'", border_size = 3, - add = TRUE, layout = c(1,3), position = c(1, 3)) +## ----eval = FALSE------------------------------------------------------------- +# # 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 +# ) + +## ----fig.width = 5, fig.height = 5, out.width = "50%", fig.align = 'center', eval = FALSE---- +# drawTreemap(tm, label_size = 2.5, label_color = "white") + +## ----fig.width = 9, fig.height = 9, out.width = "100%", fig.align = 'center', warning = FALSE, eval = FALSE---- +# drawTreemap(tm, title = "treemap 1", label_size = 2, +# color_type = "categorical", color_level = 1, +# layout = c(2, 2), position = c(1, 1), legend = TRUE) +# +# drawTreemap(tm, title = "treemap 2", label_size = 2, +# color_type = "categorical", color_level = 2, border_size = 3, +# add = TRUE, layout = c(2, 2), position = c(1, 2), legend = TRUE) +# +# drawTreemap(tm, title = "treemap 3", label_size = 2, +# color_type = "both", color_level = 1, +# add = TRUE, layout = c(2, 2), position = c(2, 1), legend = TRUE) +# +# drawTreemap(tm, title = "treemap 4", label_size = 2, +# color_type = "cell_size", color_level = 2, +# color_palette = heat.colors(10), +# border_color = grey(0.4), label_color = grey(0.4), +# add = TRUE, layout = c(2, 2), position = c(2, 2), +# title_color = "black", legend = TRUE) + +## ----message = FALSE, error = FALSE, results = 'hide', eval = FALSE----------- +# # set seed to obtain same df every time +# set.seed(123) +# df <- data.frame(A = sample(10:100, 45)) +# +# tm1 <- voronoiTreemap( +# data = df, levels = "A", +# cell_size = "A", +# shape = "rounded_rect", +# positioning = "random" +# ) +# +# tm2 <- voronoiTreemap( +# data = df, levels = "A", +# cell_size = "A", +# shape = "rounded_rect", +# positioning = "regular" +# ) +# +# tm3 <- voronoiTreemap( +# data = df, levels = "A", +# cell_size = "A", +# shape = "rounded_rect", +# positioning = "clustered" +# ) + +## ----fig.width = 12, fig.height = 4, out.width = "100%", fig.align = 'center', warning = FALSE, eval = FALSE---- +# drawTreemap(tm1, title = "positioning = 'random'", border_size = 3, +# layout = c(1,3), position = c(1, 1)) +# +# drawTreemap(tm2, title = "positioning = 'regular'", border_size = 3, +# add = TRUE, layout = c(1,3), position = c(1, 2)) +# +# drawTreemap(tm3, title = "positioning = 'clustered'", border_size = 3, +# add = TRUE, layout = c(1,3), position = c(1, 3)) ## ----------------------------------------------------------------------------- # different initial shapes, the more squared the better @@ -99,20 +99,20 @@ oct_coord <- list( y = cos(seq(0, 2, 2/8)*pi) * 1000 + 1000 ) -## ----message = FALSE, error = FALSE, results = 'hide'------------------------- -tm1 <- voronoiTreemap(data = df, levels = "A", - shape = house_coords) - -tm2 <- voronoiTreemap(data = df, levels = "A", - shape = rect_coords) - -tm3 <- voronoiTreemap(data = df, levels = "A", - shape = oct_coord) - -## ----fig.width = 12, fig.height = 4, out.width = "100%", fig.align = 'center', warning = FALSE---- -drawTreemap(tm1, layout = c(1,3), position = c(1, 1)) -drawTreemap(tm2, add = TRUE, layout = c(1,3), position = c(1, 2)) -drawTreemap(tm3, add = TRUE, layout = c(1,3), position = c(1, 3)) +## ----message = FALSE, error = FALSE, results = 'hide', eval = FALSE----------- +# tm1 <- voronoiTreemap(data = df, levels = "A", +# shape = house_coords) +# +# tm2 <- voronoiTreemap(data = df, levels = "A", +# shape = rect_coords) +# +# tm3 <- voronoiTreemap(data = df, levels = "A", +# shape = oct_coord) + +## ----fig.width = 12, fig.height = 4, out.width = "100%", fig.align = 'center', warning = FALSE, eval = FALSE---- +# drawTreemap(tm1, layout = c(1,3), position = c(1, 1)) +# drawTreemap(tm2, add = TRUE, layout = c(1,3), position = c(1, 2)) +# drawTreemap(tm3, add = TRUE, layout = c(1,3), position = c(1, 3)) ## ----------------------------------------------------------------------------- # generate data frame diff --git a/vignettes/WeightedTreemaps.Rmd b/vignettes/WeightedTreemaps.Rmd index 35d2972..17e65f9 100644 --- a/vignettes/WeightedTreemaps.Rmd +++ b/vignettes/WeightedTreemaps.Rmd @@ -1,7 +1,7 @@ --- output: rmarkdown::html_vignette title: "WeightedTreemaps" -author: "Michael Jahn, David Leslie, Ahmadou Dicko" +author: "Michael Jahn, David Leslie, Ahmadou Dicko, Paul Murrell" date: "`r Sys.Date()`" vignette: > %\VignetteIndexEntry{WeightedTreemaps} @@ -65,7 +65,7 @@ Generate the treemap. It will return a list of polygons and metadata. The columns of the data frame that are used as levels of the treemap need to be specified. Different parameters like the initial shape, or the maximum number of iterations are optional. -```{r} +```{r, eval = FALSE} # generate treemap; set seed to obtain same pattern every time tm <- voronoiTreemap( data = mtcars, @@ -78,14 +78,15 @@ tm <- voronoiTreemap( Draw the treemap. -```{r, fig.width = 5, fig.height = 5, out.width = "50%", fig.align = 'center'} +```{r, fig.width = 5, fig.height = 5, out.width = "50%", fig.align = 'center', eval = FALSE} drawTreemap(tm, label_size = 2.5, label_color = "white") ``` + The `voronoiTreemap()` and `drawTreemap()` functions are separated in order to allow drawing of the same treemap object in different ways. Computation of treemaps with thousands of cells can be very time and resource consuming (around 5-10 minutes for a 2000-cell treemap on a regular desktop computer). With the `drawTreemap()` function, we can not only plot the same treemap in different ways but also combine several treemaps on one page using the `layout` and `position` arguments. The most important style element is color. Coloring can be based on cell category, cell size, or both, using the `color_type` argument. By default, the highest hierarchical level is used for coloring but that can be customized using the `color_level` argument. -```{r, fig.width = 9, fig.height = 9, out.width = "100%", fig.align = 'center', warning = FALSE} +```{r, fig.width = 9, fig.height = 9, out.width = "100%", fig.align = 'center', warning = FALSE, eval = FALSE} drawTreemap(tm, title = "treemap 1", label_size = 2, color_type = "categorical", color_level = 1, layout = c(2, 2), position = c(1, 1), legend = TRUE) @@ -106,12 +107,13 @@ drawTreemap(tm, title = "treemap 4", label_size = 2, title_color = "black", legend = TRUE) ``` + ### Positioning of cells Generating a Voronoi treemap is an iterative and somewhat random process. Since the cells 'move' during the iteration process, it can be difficult to control the exact final position of cells. However, there are two ways to influence cell positioning. The first is to use different algorithms for sampling initial coordinates for each cell. The second is simply setting a seed, which will sample the same set of starting coordinates for the same input data. Regarding the `positioning` argument, compare the following three examples where initial positions are 1) random, 2) ordered from top to bottom, or 3) ordered from center to edges. -```{r, message = FALSE, error = FALSE, results = 'hide'} +```{r, message = FALSE, error = FALSE, results = 'hide', eval = FALSE} # set seed to obtain same df every time set.seed(123) df <- data.frame(A = sample(10:100, 45)) @@ -139,7 +141,7 @@ tm3 <- voronoiTreemap( ``` -```{r, fig.width = 12, fig.height = 4, out.width = "100%", fig.align = 'center', warning = FALSE} +```{r, fig.width = 12, fig.height = 4, out.width = "100%", fig.align = 'center', warning = FALSE, eval = FALSE} drawTreemap(tm1, title = "positioning = 'random'", border_size = 3, layout = c(1,3), position = c(1, 1)) @@ -150,6 +152,8 @@ drawTreemap(tm3, title = "positioning = 'clustered'", border_size = 3, add = TRUE, layout = c(1,3), position = c(1, 3)) ``` + + ### Custom initial shapes Instead of using predefined shapes, we can also supply a custom set of coordinates to plot a treemap using the `shape` argument. The validity of the supplied coordinates is not checked, so all responsibility lies with the user (!). The R session might even crash (due to C++ dependency) if a shape is supplied that is too irregular or edgy, and the tesselation becomes unfeasible. Here are some stable examples. @@ -172,7 +176,7 @@ oct_coord <- list( Let's generate treemaps with the shapes of a house, a wide rectangle, and an octogon. -```{r, message = FALSE, error = FALSE, results = 'hide'} +```{r, message = FALSE, error = FALSE, results = 'hide', eval = FALSE} tm1 <- voronoiTreemap(data = df, levels = "A", shape = house_coords) @@ -184,12 +188,14 @@ tm3 <- voronoiTreemap(data = df, levels = "A", ``` -```{r, fig.width = 12, fig.height = 4, out.width = "100%", fig.align = 'center', warning = FALSE} +```{r, fig.width = 12, fig.height = 4, out.width = "100%", fig.align = 'center', warning = FALSE, eval = FALSE} drawTreemap(tm1, layout = c(1,3), position = c(1, 1)) drawTreemap(tm2, add = TRUE, layout = c(1,3), position = c(1, 2)) drawTreemap(tm3, add = TRUE, layout = c(1,3), position = c(1, 3)) ``` + + ### Sunburst treemaps Sunburst treemaps are generated in the same way as described above for Voronoi treemaps. diff --git a/vignettes/WeightedTreemaps.html b/vignettes/WeightedTreemaps.html index f080a37..3a8f785 100644 --- a/vignettes/WeightedTreemaps.html +++ b/vignettes/WeightedTreemaps.html @@ -10,9 +10,9 @@ - + - + WeightedTreemaps @@ -339,8 +339,9 @@

WeightedTreemaps

-

Michael Jahn, David Leslie, Ahmadou Dicko

-

2023-10-05

+

Michael Jahn, David Leslie, Ahmadou Dicko, Paul +Murrell

+

2024-01-07

@@ -430,7 +431,7 @@

Voronoi treemaps

)

Draw the treemap.

drawTreemap(tm, label_size = 2.5, label_color = "white")
-

+

The voronoiTreemap() and drawTreemap() functions are separated in order to allow drawing of the same treemap object in different ways. Computation of treemaps with thousands of @@ -462,7 +463,7 @@

Voronoi treemaps

border_color = grey(0.4), label_color = grey(0.4), add = TRUE, layout = c(2, 2), position = c(2, 2), title_color = "black", legend = TRUE) -

+

Positioning of cells

@@ -508,7 +509,7 @@

Positioning of cells

drawTreemap(tm3, title = "positioning = 'clustered'", border_size = 3, add = TRUE, layout = c(1,3), position = c(1, 3))
-

+

Custom initial shapes

@@ -545,7 +546,7 @@

Custom initial shapes

drawTreemap(tm1, layout = c(1,3), position = c(1, 1))
 drawTreemap(tm2, add = TRUE, layout = c(1,3), position = c(1, 2))
 drawTreemap(tm3, add = TRUE, layout = c(1,3), position = c(1, 3))
-

+

Sunburst treemaps