Returns the computed values behind each layer — post-stat, post-position, post-facet — in data units (never normalized coordinates). For a geom_point() layer that is the x/y you supplied; for geom_histogram() it is the bin centers, counts, and bin edges actually drawn; for geom_boxplot() the quartiles and whisker ends.

plot_data(plot, layer = NULL, panel = NULL)

Arguments

plot

A GgnextPlot.

layer

Which layer to return: an integer index, or NULL (the default) for a list of all layers. With one layer, the data frame itself is returned rather than a one-element list.

panel

Which facet panel to return: an integer index, NULL (the default) for all panels combined with a panel column, or "list" to get a list per panel.

Value

A data frame, or a list of data frames.

Details

Only columns the layer genuinely uses are returned. Constant grouping columns are dropped, so a plot with no group aesthetic has no group column.

Examples

p <- ggnext(cars, aes(speed, dist)) + geom_point()
head(plot_data(p))
#>   x  y
#> 1 4  2
#> 2 4 10
#> 3 7  4
#> 4 7 22
#> 5 8 16
#> 6 9 10

# Stats: what geom_histogram() actually drew.
h <- ggnext(cars, aes(speed)) + geom_histogram(bins = 5)
plot_data(h)
#>      x  y ymin ymax xmin xmax
#> 1  6.1  5    0    5  4.0  8.2
#> 2 10.3 10    0   10  8.2 12.4
#> 3 14.5 13    0   13 12.4 16.6
#> 4 18.7 15    0   15 16.6 20.8
#> 5 22.9  7    0    7 20.8 25.0

# Facets keep a panel column.
f <- ggnext(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point() + facet_wrap(Species)
head(plot_data(f))
#>    panel   x   y
#> 1 setosa 5.1 3.5
#> 2 setosa 4.9 3.0
#> 3 setosa 4.7 3.2
#> 4 setosa 4.6 3.1
#> 5 setosa 5.0 3.6
#> 6 setosa 5.4 3.9