Foreword

  • Output options: the ‘tango’ syntax and the ‘readable’ theme.
  • Snippets and results.

1, The FlowingData Map

FlowingData proposes a tool for building treemaps. Originally design for summarizing a security portfolio, the portfolio package function can display any kind of data.

Print the head of a fake dataset.

head(data, 10)
##      id  views comments                  category
## 1  5019 148896       28    Artistic Visualization
## 2  1416  81374       26             Visualization
## 3  1416  81374       26                  Featured
## 4  3485  80819       37                  Featured
## 5  3485  80819       37                   Mapping
## 6  3485  80819       37              Data Sources
## 7   500  76495       10 Statistical Visualization
## 8   500  76495       10                   Mapping
## 9   500  76495       10     Network Visualization
## 10 4092  66650       70        Ugly Visualization

Build a treemap.

library(portfolio)

map.market(id = data$id, 
           area = data$views, 
           group = data$category, 
           color = data$comments, 
           main = "FlowingData Map")

2, Canada Federal Budget with portfolio

Collect de data. Source: Tax Alert – Canada, Federal Budget 2016-17, EY, 22 March 2016. After preprocessing the data…

Print the head of the dataset.

head(budget)
##            RevExp     Category                   Details Budget2016
## 1 Revenue outlook Income taxes    Income taxes, Personal      143.9
## 2 Revenue outlook Income taxes   Income taxes, Corporate       37.9
## 3 Revenue outlook Income taxes Income taxes, Nonresident        6.3
## 4 Revenue outlook Excise taxes         Excise taxes, GST       33.5
## 5 Revenue outlook Excise taxes     Excise taxes, Customs        5.0
## 6 Revenue outlook Excise taxes      Excise taxes, Duties       11.1

Split the revenues and expenses.

revenues <- subset(budget, budget$RevExp == 'Revenue outlook')

# Print the revenues
revenues[, c(3, 4)]
##                     Details Budget2016
## 1    Income taxes, Personal      143.9
## 2   Income taxes, Corporate       37.9
## 3 Income taxes, Nonresident        6.3
## 4         Excise taxes, GST       33.5
## 5     Excise taxes, Customs        5.0
## 6      Excise taxes, Duties       11.1
## 7               EI premiums       22.4
## 8            Other revenues       27.7
expenses <- subset(budget, budget$RevExp == 'Program expenses outlook')

# Print the expenses
expenses[, c(3, 4)]
##                                      Details Budget2016
## 9                           Elderly benefits      -48.4
## 10                               EI benefits      -21.1
## 11                       Children's benefits      -21.9
## 12 Major transfers to other levels of gov’ts      -68.6
## 13                   Direct program expenses     -131.3

Build a revenue treemap. Save the plot.

revtreemap <- map.market(id = rep(1, nrow(revenues)),
                         area = revenues$Budget2016, 
                         group = revenues$Detail, 
                         color = revenues$Budget2016, 
                         main = "Revenues",
                         scale = max(revenues$Budget2016),
                         lab = c(TRUE, FALSE))

revtreemap
## gTree[MAPMARKET]
dev.copy(png,'img/revtreemap.png')
## png 
##   3
dev.off()
## png 
##   2

Build an expense treemap. Save the plot.

exptreemap <- map.market(id = rep(1, nrow(expenses)),
                         area = -expenses$Budget2016, 
                         group = expenses$Detail, 
                         color = expenses$Budget2016, 
                         main = "Expenses",
                         scale = -min(expenses$Budget2016),
                         lab = c(TRUE, FALSE))

exptreemap
## gTree[MAPMARKET]
dev.copy(png,'img/exptreemap.png')
## png 
##   3
dev.off()
## png 
##   2

Display the revenues and expenses side-to-side.

3, Canada Federal Budget with treemap

The package allows different displays. However, we have to work with positive numbers for the expenses. Convert the data.

revenues$Revenues_2016 <- revenues$Budget2016
expenses$Expenses_2016 <- -expenses$Budget2016

The index display

With categories (over) and details (under).

library(treemap)

# index, value, comp, dens, depth, color, manual
treemap(revenues,
        index = c('Category','Details'),
        vSize = 'Revenues_2016',
        vColor = 'Revenues_2016',
        type = 'index')

treemap(expenses,
        index = c('Category','Details'),
        vSize = 'Expenses_2016',
        vColor = 'Expenses_2016',
        type = 'index')

Categories only.

# index, value, comp, dens, depth, color, manual
treemap(revenues,
        index = c('Category'),
        vSize = 'Revenues_2016',
        vColor = 'Revenues_2016',
        type = 'index')

treemap(expenses,
        index = c('Category'),
        vSize = 'Expenses_2016',
        vColor = 'Expenses_2016',
        type = 'index')

Details only.

# index, value, comp, dens, depth, color, manual
treemap(revenues,
        index = c('Details'),
        vSize = 'Revenues_2016',
        vColor = 'Revenues_2016',
        type = 'index')

treemap(expenses,
        index = c('Details'),
        vSize = 'Expenses_2016',
        vColor = 'Expenses_2016',
        type = 'index')

The value display

Details only, gradual tones of green, and a scale.

treemap(revenues,
        index = c('Details'),
        vSize = 'Revenues_2016',
        vColor = 'Revenues_2016',
        type = 'value')

treemap(expenses,
        index = c('Details'),
        vSize = 'Expenses_2016',
        vColor = 'Expenses_2016',
        type = 'value')

The dens display

Categories only, gradual tones of red, and a scale.

treemap(revenues,
        index = c('Category'),
        vSize = 'Revenues_2016',
        vColor = 'Revenues_2016',
        type = 'dens')

treemap(expenses,
        index = c('Category'),
        vSize = 'Expenses_2016',
        vColor = 'Expenses_2016',
        type = 'dens')

The color display

Details only.

treemap(revenues,
        index = c('Details'),
        vSize = 'Revenues_2016',
        vColor = 'Revenues_2016',
        type = 'color')

treemap(expenses,
        index = c('Details'),
        vSize = 'Expenses_2016',
        vColor = 'Expenses_2016',
        type = 'color')