Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Treemap - Add total value to treemap label

Tags:

r

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 
like image 718
M.Viking Avatar asked Dec 06 '25 05:12

M.Viking


1 Answers

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

like image 165
M.Viking Avatar answered Dec 08 '25 19:12

M.Viking