wilkox / treemapify

🌳 Draw treemaps in ggplot2
http://wilkox.org/treemapify
213 stars 18 forks source link

Using ggsave does not include the text created by geom_treemap_text #45

Closed pprevos closed 3 years ago

pprevos commented 3 years ago

I need to save the treemap as an image, but when I do so, any text is not included. This is a minimum working example":

df <- data.frame(Channel = c("Twitter", "Facebook", "YouTube"),
                 Engagement = c(1500, 150, 450))
ggplot(df, aes(area = Engagement, fill = Channel, label = Engagement)) + 
  geom_treemap() +
  scale_fill_manual(name = NULL, values = c("#3b5998", "#1DA1F2", "#FF0000")) +
  geom_treemap_text(colour = "white", place = "centre", grow = TRUE,
                    padding.x = unit(16, "mm"))
ggsave("social-media.png", width = 2.9, height = 1.3)

The treemap itself is saves, but the labels have vanished. When I remove the width and height variables, the files saves with the relevant text.

wilkox commented 3 years ago

The problem is coming from padding.x = unit(16, "mm")). This puts so much padding between the text and the boundaries of the tile that when it is drawn at a small absolute size there is no space at all for the text. ggsave() uses inches by default so the total image is only 74 by 33 mm. If you remove this argument, or specify a much smaller padding, it should work.