M.Viking
M.Viking

Reputation: 5398

R Treemap - Add total value to treemap label

Treemap uses vSize value to scale the size of each box.

I want to printed the value of the sum amount in the treemap plot label.

Current code:

library(treemap)
treemap(dtf=iris, index="Species", vSize="Sepal.Length")

treemap

xtabs(Sepal.Length ~ Species, data = iris)
# Species
# setosa versicolor  virginica 
# 250.3      296.8      329.4 

Upvotes: 3

Views: 3389

Answers (1)

M.Viking
M.Viking

Reputation: 5398

Summarize the data and create a new index and label.

library(treemap)
library(dplyr)
iris%>%
  group_by(Species)%>%
  summarise(Sum.Sepal.Length=sum(Sepal.Length))%>%
  mutate(Species.Index=paste(Species, Sum.Sepal.Length, sep ="\n"))%>%
  treemap(index="Species.Index", vSize="Sum.Sepal.Length")

treemap with values in labels

Upvotes: 4

Related Questions