Search code examples
rggplot2treemap

How to change label color/size in 'treemapify'


In 'treemapify', i use function 'paste0' to combine three variable as the label, now i want change the font color and size separately (as the comment in below code). Anyone can help on this ? Thanks!

library(ggplot2)
library(treemapify)
library(formattable)
plot_data <- data.frame(
  region=c("a","b","c","d","e","f"),
  amount=c(3,2,1,7,2,4),
 increase=c(0.5,0.1,0.7,0.4,0.3,0.9))

plot_data %>% ggplot(aes(area=amount,fill=region,
                         label=paste0(region, #' want the font color  'darkblue'
                                      '\n',
                                      comma(amount,1), # want the font color  'grey60'
                                      '\n',
                                      percent(increase,1) # want the font color  'white'
                                      )))+
  geom_treemap()+geom_treemap_text()

enter image description here


Solution

  • This is a fairly pedestrian solution, but it should give you the flexibility you need:

    ggplot(plot_data, aes(area = amount, fill = region)) +
      geom_treemap() + 
      geom_treemap_text(aes(label = region), color = "darkblue") +
      geom_treemap_text(aes(label = scales::comma(amount, 1)), 
                        color = "grey60", padding.y = unit(10, "mm")) +
      geom_treemap_text(aes(label = scales::percent(increase, 1)), color = "white",
                        padding.y = unit(20, "mm"))
    

    enter image description here