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
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")
Could someone assist me in achieving this effect(white line or empty spaces) between rectangles ?



treemapifydocs? This is a really minor modification to my answer to your almost identical question earlier today.