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:
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With