Creation of a treemap chart object that can be inserted in a 'Microsoft' document. Treemap charts use the chartEx pipeline (Office 2016+); older versions of 'Microsoft Office' will display a fallback placeholder.
Data is hierarchical: one column per level (parent to leaf, left to right) and one numeric column for the leaf values.
Per-leaf coloring with chart_data_fill()
Per-leaf coloring via chart_data_fill() with a named vector works
correctly on flat treemaps (length(path) == 1). With a hierarchy
(length(path) >= 2), PowerPoint silently re-maps <cx:dataPt>
indices in a way that drops idx="0" and applies the last specified
color to remaining leaves. This is a PowerPoint rendering limitation
(also reproducible from Excel-generated chartEx files); the mschart
XML output is conformant. For hierarchical treemaps, prefer a single
fill color via chart_data_fill(x, "#HEX").
See also
Examples
library(officer)
dat <- data.frame(
region = c("EU", "EU", "EU", "AM", "AM"),
country = c("FR", "FR", "DE", "US", "US"),
city = c("Paris", "Lyon", "Berlin", "NYC", "LA"),
value = c(10, 5, 12, 20, 8),
stringsAsFactors = FALSE
)
tm <- ms_treemapchart(
data = dat, path = c("region", "country", "city"), value = "value"
)
tm <- chart_labels(tm, title = "Sales by region")
doc <- read_pptx()
doc <- add_slide(doc)
#> Warning: Calling `add_slide()` without specifying a `layout` is deprecated.
#> Please pass a `layout` or use `layout_default()` to set a default.
#> => I will now continue with the former `layout` default "Title and Content" for backwards compatibility...
doc <- ph_with(doc, tm, location = ph_location_fullsize())
print(doc, target = tempfile(fileext = ".pptx"))
