0

Shown below is a treemap created with Plotly. You can find the corresponding code and chart below.

I appreciate the style of this graph and aim to replicate it using ggplot2. Below, you'll see a treemap generated with ggplot2.

library(ggplot2)
library(plotly)
library(dplyr)

df1<-structure(list(manuf = c("AMC", "Cadillac", "Camaro", "Chrysler", 
                              "Datsun", "Dodge", "Duster", "Ferrari", "Fiat", "Ford", "Honda", 
                              "Hornet", "Lincoln", "Lotus", "Maserati", "Mazda", "Merc", "Pontiac", 
                              "Porsche", "Toyota", "Valiant", "Volvo"), count = c(1L, 1L, 1L, 
                                                                                  1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 7L, 1L, 1L, 
                                                                                  2L, 1L, 1L)), row.names = c(NA, -22L), class = c("tbl_df", "tbl", 
                                                                                                                                   "data.frame"))
df1$test<-"CARS"

treemap<-plot_ly(data = df1,
                 type= "treemap",
                 values= ~count,
                 labels= ~manuf,
                 parents=  ~test,
                 domain= list(column=0),
                 name = " ",
                 textinfo="label+value+percent parent")  %>%
  layout(title="", 
         annotations =
           list(x = 0, y = -0.1,
                title = "", 
                text = " ",
                showarrow = F,
                xref='paper',
                yref='paper'))

treemap

enter image description here

However, I'm encountering difficulty reproducing the white lines or empty space that separate the rectangles in Plotly within ggplot2. Below you can see my Treemap with ggplot2.

library(treemapify)
library(ggplot2)

group <- paste("Group", 1:9)
subgroup <- c("A", "C", "B", "A", "A",
              "C", "C", "B", "B")
value <- c(7, 25, 50, 5, 16,
           18, 30, 12, 41)

df <- data.frame(group, subgroup, value) 

# Calculate percentage share for each group
df$percentage <- df$value / sum(df$value) * 100

test_treemap<-ggplot(df, aes(area = percentage, fill = group, label = paste(subgroup, ": ", value, " (", round(percentage, 2), "%)"))) +
  geom_treemap() +
  geom_treemap_text(colour = "white",
                    hjust = "left",
                    #place = "left",
                    size = 8,
                    fontface = "bold") +
  scale_fill_brewer(palette = "Set1")+
  labs(title = "Treemap with Percentage Share by Group")+
  theme(legend.position = "none")

enter image description here

Could someone assist me in achieving this effect(white line or empty spaces) between rectangles ?

1
  • 2
    Did you glance at the treemapify docs? This is a really minor modification to my answer to your almost identical question earlier today. Commented Mar 26, 2024 at 13:58

1 Answer 1

2

You can add an outline using color= and size= in geom_treemap. I also added some linebreaks to your labels and some more padding:

library(treemapify)
library(ggplot2)

ggplot(
  df,
  aes(
    area = percentage, fill = group,
    label = paste0(subgroup, ":\n", value, "\n", round(percentage, 2), "%")
  )
) +
  geom_treemap(color = "white", size = 4) +
  geom_treemap_text(
    colour = "white",
    size = 8,
    fontface = "bold",
    padding.x = unit(2, "mm"),
    padding.y = unit(2, "mm")
  ) +
  scale_fill_brewer(palette = "Set1") +
  labs(title = "Treemap with Percentage Share by Group") +
  theme(legend.position = "none")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.