Skip to content

Commit

Permalink
reduced building time
Browse files Browse the repository at this point in the history
  • Loading branch information
m-jahn committed Jan 8, 2024
1 parent e8bff53 commit be5fd74
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 98 deletions.
164 changes: 82 additions & 82 deletions vignettes/WeightedTreemaps.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
22 changes: 14 additions & 8 deletions vignettes/WeightedTreemaps.Rmd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -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,
Expand All @@ -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")
```

<img src="png/unnamed-chunk-4-1.png" align="center" width="50%" />

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)
Expand All @@ -106,12 +107,13 @@ drawTreemap(tm, title = "treemap 4", label_size = 2,
title_color = "black", legend = TRUE)
```

<img src="png/unnamed-chunk-5-1.png" align="center" width="100%" />

### 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))
Expand Down Expand Up @@ -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))
Expand All @@ -150,6 +152,8 @@ drawTreemap(tm3, title = "positioning = 'clustered'", border_size = 3,
add = TRUE, layout = c(1,3), position = c(1, 3))
```

<img src="png/unnamed-chunk-7-1.png" align="center" width="100%" />

### 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.
Expand All @@ -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)
Expand All @@ -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))
```

<img src="png/unnamed-chunk-10-1.png" align="center" width="100%" />

### Sunburst treemaps

Sunburst treemaps are generated in the same way as described above for Voronoi treemaps.
Expand Down
17 changes: 9 additions & 8 deletions vignettes/WeightedTreemaps.html

Large diffs are not rendered by default.

0 comments on commit be5fd74

Please sign in to comment.