gginnards (version 0.0.2)

drop_vars: Explore and manipulate the embedded data.

Description

Automatically remove unused variables from the "default" data object embedded in a gg or ggplot object with drop_vars().

Usage

drop_vars(p, keep.vars = character(), guess.vars = TRUE)

mapped_vars(p, invert = FALSE)

data_vars(p)

data_attributes(p)

Arguments

p

ggplot Plot object with embedded data.

keep.vars

character Names of unused variables to be kept.

guess.vars

logical Flag indicating whether to find used variables automatically.

invert

logical If TRUE return indices for elements of data that are not mapped to any aesthetic or facet.

Value

character vector with names of mapped variables in the default data object.

character vector with names of all variables in the default data object.

list containing all attributes of the default data object.

Warning!

The current implementation drops variables only from the default data object. Data objects within layers are not modified.

Examples

Run this code
# NOT RUN {
library(ggplot2)

p <- ggplot(mpg, aes(factor(year), (cty + hwy) / 2)) +
  geom_boxplot() +
  facet_grid(. ~ class)

mapped_vars(p) # those in use
mapped_vars(p, invert = TRUE) # those not used

p.dp <- drop_vars(p) # we drop unused vars

# number of columns in the data member
ncol(p$data)
ncol(p.dp$data)

# which vars are in the data member
data_vars(p)
data_vars(p.dp)

# which variables in data are used in the plot
mapped_vars(p)
mapped_vars(p.dp)

# the plots identical
p
p.dp

# structure and size of p
str(p, max.level = 0)
str(p.dp, max.level = 0) # smaller in size

# structure and size of p["data"]
str(p, components = "data")
str(p.dp, components = "data") # smaller in size

# }

Run the code above in your browser using DataLab