Search code examples
rlabeltreemap

R Treemap - how to add multiple labels


I'm trying to create a treemap using R package treemap similar to the one in a sample from the package.

library(treemap)
data(GNI2010)
treemap(GNI2010,
    index=c("iso3"),
    vSize="population",
    vColor="GNI",
    type="value")

Is there any way to add extra labels from columns to display "CHN, 1,35 bln, 20% "


Solution

  • You could create a new variable, such as "label" below, and with paste or sprintf and various format choices for digits, etc., craft whatever label you want. Then use that variable instead of iso3. You will run out of space on the smaller rectangles but there is the force.print.labels argument to invoke.

    Here is a simple example that adds the population.enter image description here

    GNI2010$label <- paste(GNI2010$iso3, GNI2010$population, sep = ", ")
    
    treemap(GNI2010,
            index=c("label"),
            vSize="population",
            vColor="GNI",
            type="value")