slim_data: Slim unused data in the plot

View source: R/slim_data.R

slim_dataR Documentation

Slim unused data in the plot

Description

Remove unused columns from the data in a ggplot or patchwork object. This function keeps only the columns that are actually used in the plot (e.g., in mappings, aesthetics, or facets), which can significantly reduce the object size when the original data contains many unused columns.

Usage

slim_data(p)

## S3 method for class 'ggplot'
slim_data(p)

## S3 method for class 'patchwork'
slim_data(p)

Arguments

p

A ggplot object or a patchwork object.

Value

A ggplot or patchwork object with unused data columns removed.

Examples

library(ggplot2)
p <- ggplot(
  data = mtcars,
  aes(x = mpg, y = wt, colour = cyl)
) +
  geom_point()
object.size(p)
colnames(p$data)

p_slim <- slim_data(p)
object.size(p_slim)
colnames(p_slim$data)

thisplot documentation built on March 7, 2026, 5:07 p.m.