% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot-data.R
\name{plot_data}
\alias{plot_data}
\title{Extract the exact data a plot draws}
\usage{
plot_data(plot, layer = NULL, panel = NULL)
}
\arguments{
\item{plot}{A \link{GgnextPlot}.}

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

\item{panel}{Which facet panel to return: an integer index, \code{NULL} (the
default) for all panels combined with a \code{panel} column, or \code{"list"} to
get a list per panel.}
}
\value{
A data frame, or a list of data frames.
}
\description{
Returns the computed values behind each layer — post-stat, post-position,
post-facet — in data units (never normalized coordinates). For a
\code{geom_point()} layer that is the x/y you supplied; for \code{geom_histogram()}
it is the bin centers, counts, and bin edges actually drawn; for
\code{geom_boxplot()} the quartiles and whisker ends.
}
\details{
Only columns the layer genuinely uses are returned. Constant grouping
columns are dropped, so a plot with no \code{group} aesthetic has no \code{group}
column.
}
\examples{
p <- ggnext(cars, aes(speed, dist)) + geom_point()
head(plot_data(p))

# Stats: what geom_histogram() actually drew.
h <- ggnext(cars, aes(speed)) + geom_histogram(bins = 5)
plot_data(h)

# Facets keep a panel column.
f <- ggnext(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point() + facet_wrap(Species)
head(plot_data(f))
}
