简体   繁体   中英

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

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. 在此处输入图片说明

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM