Pages Menu
TwitterRss
Categories Menu

Posted by on Aug 17, 2011 in R Spatial, Resources, Visualisation | 6 comments

Improved Tree Maps with R

“Treemaps display hierarchical (tree-structured) data as a set of nested rectangles. Each branch of the tree is given a rectangle, which is then tiled with smaller rectangles representing sub-branches. A leaf node’s rectangle has an area proportional to a specified dimension on the data. Often the leaf nodes are colored to show a separate dimension of the data.

When the color and size dimensions are correlated in some way with the tree structure, one can often easily see patterns that would be difficult to spot in other ways, for example, if a certain color is particularly relevant. A second advantage of treemaps is that, by construction, they make efficient use of space. As a result, they can legibly display thousands of items on the screen simultaneously.” (source: Wikipedia)

I think treemaps are becoming an increasingly popular method of visualising multidimensional datasets and there are many ways to create them. This tutorial is going to focus on the use of R and builds on the one published on flowingdata.

Open R and install the following packages

install.packages("portfolio")
install.packages("RColorBrewer")

now load them.

library(RColorBrewer)
library(portfolio)

The next step is to load in the data file we are using. This is an edited version of the London Borough Profiles csv taken from the London Datastore. There are five columns of data. The three we are interested in are “pop”, “earnings” and “party”.

input<-read.csv("http://spatialanalysis.co.uk/wp-content/uploads/2011/08/tree_eg_data.csv")
attach(input)

A treemap generally requires 4 pieces of information:
the item- in this case the London Borough's or "id"- each will be assigned a rectangle,
a value to scale the size of the rectangle by- in this case the population or "pop",
a value for assigning the colour- in this case the average earnings per person or "earnings",
and a broader group to which the item belongs- in this case the ruling political party or "party".

Armed with this we can simply used the map.market function from the portfolio package (installed earlier) to produce a treemap.

map.market(id, pop, party, earnings, lab = c(TRUE, TRUE), main="London Earnings, Population and Politics")

The output looks OK but I don't really like the colours. I have therefore edited the code so that a selection of colours can be used using the ColorBrewer palettes. You can either download the code or load it straight away with
source("http://dl.dropbox.com/u/10640416/treemapbrewer.r")

you have now loaded in a new function called "treemap" that does a very similar job to the one above but has a few other options that you can see used below

treemap(id, pop, party, earnings, lab = c(TRUE, TRUE), main="London Earnings, Population and Politics", pal="Oranges", linecol= "dark gray", textcol="white")

The plot above used the "Oranges" palette but there are many more such as "Blues", "BuPu" and "Reds". Try for example
treemap(id, pop, party, earnings, lab = c(TRUE, TRUE), main="London Earnings, Population and Politics", pal="Blues", linecol= "white", textcol="black")

When you are happy with the results save the plot as a pdf
pdf("my_tree_map2.pdf")
treemap(id, pop, party, earnings, lab = c(TRUE, TRUE), main="London Earnings, Population and Politics", pal="Oranges", linecol= "dark gray")
dev.off()

or PNG
png("my_tree_map2.png")
treemap(id, pop, party, earnings, lab = c(TRUE, TRUE), main="London Earnings, Population and Politics", pal="Oranges", linecol= "dark gray")
dev.off()

and then you can edit it using image/ vector editing software such as GIMP or Inkscape to get the following result:

I hope that the options to change colours makes for a more interesting treemaps than the standard red/green ones we are used to seeing. If anyone knows how to alter the scale bar so that it does not show values beyond the range of the data it would be great to see how it is done!

Share on Facebook
Bookmark this on Google Bookmarks
Share on reddit
Bookmark this on Digg
Share on StumbleUpon
Share on LinkedIn

6 Comments

  1. James, interesting visualisations but I fear some what unnecessary. Why not a simple table instead of all this palava? Have four columns: Borough name / population / average earnings / lead party. List the data in earnings order. With a table the boroughs would be identified, the average earnings more precise.
    I think you’re starting with a tool and looking for applications. Never an excellent idea – ask any carpenter. (Sorry to be so critical; impatient in my old age.)

  2. Hi,
    Like your tutorial and I think that the visualization alternative of a treemap is very easy to read.
    I only have one question:
    How to change the scale? I’ve been looking on internet and reading the function and have no clue at all!
    Thanks in advance
    Many

    • Thanks to my father who shared with me concerning this weblog,
      this weblog is truly awesome.

  3. this is f***ing great !!!

  4. This is great. I wonder if it would make sense to have a similar treemap where the color changes by group (so that labor is red, conservative blue, lib dem yellow and no control gray). And then a color scale legend that includes the 4 colors.

    Regards,

    Javier

  5. This solution is simply great!!
    congratulations

Post a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>