Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Treemap labels don't justify when using fontsize.labels set to 0 for top level labels?

Tags:

r

treemap

I am trying to remove the top-level labels on a R treemap created using version 2.4 on R version 3.1 on a Macbook Air running Mavericks.

Run the following example from the treemap documentation:

require(treemap)
data(business)
business <- transform(business, data.available = factor(!is.na(turnover)), x = 1)
tm <- treemap(business,
              index=c("NACE1", "NACE2"),
              vSize="x",
              vColor="data.available",
              type="categorical")

This is the resulting treemap:

correct treemap

I want to remove the top level labels, e.g. "C - Manufacturing", etc. using the fontsize.labels argument which says:

"Use value 0 to omit the labels for the corresponding aggregation level."

When I try this:

tm <- treemap(business,
              index=c("NACE1", "NACE2"),
              vSize="x",
              vColor="data.available",
              type="categorical",
              fontsize.labels = c(0,16))

the top level labels are indeed removed but the second level labels are no longer justifying correctly - the fontsize is reduced so that the text fits all in one line, which makes it difficult to read.

treemap with fontsize.labels

I've tried using inflate.labels but this is a single logical value that applies to all levels (my use case has 3 nested levels and I only want the top level labels removed) and various combinations of lowerbound.cex.labels, force.print.labels but nothing seems to get the result I'm looking for.

Found this answer from 2.5 years ago: How do I omit labels in the R treemap? but I'd prefer to avoid post-processing the treemap after it has been plotted (I am sending these to pdf so I don't want to post-process the pdf file afterward). Plus, I'd prefer to avoid running code that is dependent upon the internal structure of the treemap created.

Seems like fontsize.labels should do what I need - just can't get the second level labels to justify (wrap) correctly. Am I missing anything?

like image 340
Mac471 Avatar asked Nov 09 '22 02:11

Mac471


1 Answers

I believe this is due to a bug that causes all label wrapping to be based on the first element in the fontsize.labels vector, rather than the element that relates to the level in question. One workaround to this problem is to set the fontcolor.labels and bg.labels arguments in the treemap function to make the labels transparent (note that bg.labels can either be a colour name or a value from 0 to 255 indicating opacity). In your example this would be:

tm <- treemap(business,
              index=c("NACE1", "NACE2"),
              vSize="x",
              vColor="data.available",
              type="categorical",
              fontsize.labels = 16,
              fontcolor.labels=c("transparent", "black"),
              bg.labels=0
)

The only drawback is that this will prevent treemap from automatically assigning a label colour based on the background of the rectangle.

like image 157
Oliver Higgs Avatar answered Nov 15 '22 06:11

Oliver Higgs