r_user
r_user

Reputation: 21

How do I change the position of labels in the R treemap?

I've made a treemap using the R treemap package with 2 levels of hierarchy.

My problem is that some of the labels overlap, so you can't really see the lower level hierarchy label. In the example from the man page (see below), you can't see the country label for Ukraine because the continent label (Europe) is on top of it. I know you can make the labels smaller font, but is there a way to move the position of the labels? For example, could I move the continent labels to the upper left corner of the continent boxes so I can see all the country names?

require(treemap)
data(GNI2010)

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

Upvotes: 2

Views: 3667

Answers (2)

kalebo
kalebo

Reputation: 372

This is now possible with recent versions of treemap using the align.labels option. There are several other new options that might help as well.

In the documentation for align.list it says:

object that specifies the alignment of the labels. Either a character vector of two values specifying the horizontal alignment ("left", "center", or "right") and the vertical alignment ("top", "center", or "bottom"), or a list of such character vectors, one for each aggregation level.

One thing to note is that the order in each of the character vectors matters. That is, the value that corresponds to the x axis location comes first and then the value y axis location. E.g., c("right", "bottom") is valid but c("bottom", "right") is not.

Here is your original example corrected so the labels do not overlap.

data(GNI2014)
treemap(GNI2014,
         index=c("continent", "iso3"),
         vSize="population",
         vColor="GNI",
         type="value", 
         align.labels = list(c("left", "top"), c("right", "bottom"))
)

Which yields the following plot: non-overlapping labels

Upvotes: 5

Martijn Tennekes
Martijn Tennekes

Reputation: 2051

Unfortunately it's not possible yet. This feature is listed for a next update, but with low priority.

There might be a difficult way to do this: by manually tweaking the plot afterward with the grid package. You could try to find the continent labels with grid.ls(), and move those.

Upvotes: 0

Related Questions