tr12
tr12

Reputation: 21

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% "

Upvotes: 2

Views: 7969

Answers (2)

Angel Lopez Sanz
Angel Lopez Sanz

Reputation: 131

Following lawyeR answer, but adding a detail. If you want to center both labels one above the other, use "\n" as separator in paste function

GNI2010$label <- paste(GNI2010$iso3, GNI2010$population, sep = "\n")

treemap(GNI2010,
    index=c("label"),
    vSize="population",
    vColor="GNI",
    type="value")

Upvotes: 6

lawyeR
lawyeR

Reputation: 7664

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")

Upvotes: 10

Related Questions