Search code examples
rggplot2treemap

Plotting a Tree Map in R using treemapify


Im making a treemap of some data using a pretty cool library called treemapifyof which the details can be found here and github repository here

Based on my reading of the documentation it seems to be based on ggplot2 so it should be possible to modify the graph using the grammar of graphics

My code is below with some made up data. The end result is pretty nice but i want to change the color scheme to a more subtle using the line scale_colour_brewer. The graph runs fine but the colour scheme seems to be ignored. Has anyone had any experience with this?

# Create Random Data
country <- c("Ireland","England","France","Germany","USA","Spain")
job <- c("IT","SOCIAL","Project Manager","Director","Vice-President")

mydf <- data.frame(countries = sample(country,100,replace = TRUE),
               career = sample(job,100,replace=TRUE),
               participent = sample(1:100, replace = TRUE)
)

# Set Up the coords
treemap_coords <- treemapify(mydf, 
                         area="participent", 
                         fill="countries", 
                         label="career", 
                         group="countries")

# Plot the results using the Green Pallete
ggplotify(treemap_coords, 
      group.label.size.factor = 2,
      group.label.colour = "white",
      label.colour = "black",
      label.size.factor = 1) + 
  labs(title="Work Breakdown") +
  scale_colour_brewer(palette = "Greens")

Solution

  • If you want to change the fill color of the rectangles, try the scale for fill instead the one for colour:

    scale_fill_brewer(palette = "Greens")