Search code examples
rggplot2treemap

Set Treemapify subgroup colours in R


I want to set the specific colours for subgroups using Treemapify.

Here's a reproducible code sample

library(ggplot2)
library(treemapify)

name <- c("France", "Germany", "Chad", "Mali")
population <- c(100, 200, 300, 400)
parent <- c("Europe","Europe","Africa","Africa")
df <- data.frame(name, population, parent)
ggplot(df, aes(area = population, label = name, fill=parent, subgroup=parent)) +
geom_treemap() +
geom_treemap_text(colour = "white", place = "centre", grow = TRUE)

The colours are set by the fill, by default, they look like this:

enter image description here

I've tried changing fill=c("green","green","yellow","yellow") and fill=c("#f00","#f00","#ff0","#ff0")

But neither of those produce the colour I've specified.

I've tried manually specifying by adding scale_fill_manual(values=c("#ff0000", "#ff0000", "#00ff00", "#00ff00"))

But that doesn't render any colour.

How do I specify the colour for my sub-groups?


Solution

  • Ah I'm glad that solved the problem :) Answer from the comments:

    scale_fill_manual(values=c("#ff0000", "#00ff00"))