Skip to content

ith_data returns the data set used by the ith layer.

Usage

ith_data(p, i, local_only = FALSE)

Arguments

p

A ggplot object or a layer extracted from a ggplot object with get_geom_layer.

i

A numerical index that corresponds to the first layer of a plot (1), the second layer (2), and so on.

local_only

TRUE or FALSE. See the details.

Value

A data frame. If no data set is found, ith_data returns NULL.

Details

If local_only = TRUE, ith_data returns the data set, if any, that was defined locally in the function that created the ith layer. If local_only = FALSE, ith_data returns the data used by the ith layer, whether or not that data was defined globally in ggplot or locally.

Functions that use the ith_ prefix are designed to eliminate the need to call get_geom_layer to check a specific layer in a plot, e.g. p %>% get_geom_layer(geom = "point") %>% get_data().

See also

Other functions for checking data: get_data(), ith_data_is(), uses_data()

Examples

require(ggplot2)
d2 <- head(mpg)
p <- ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
  geom_point(data = d2, color = "red") +
  geom_point()
ith_data(p, i = 1)
#> # A tibble: 6 × 11
#>   manufacturer model displ  year   cyl trans drv     cty   hwy fl    class
#>   <chr>        <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
#> 1 audi         a4      1.8  1999     4 auto… f        18    29 p     comp…
#> 2 audi         a4      1.8  1999     4 manu… f        21    29 p     comp…
#> 3 audi         a4      2    2008     4 manu… f        20    31 p     comp…
#> 4 audi         a4      2    2008     4 auto… f        21    30 p     comp…
#> 5 audi         a4      2.8  1999     6 auto… f        16    26 p     comp…
#> 6 audi         a4      2.8  1999     6 manu… f        18    26 p     comp…