% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ggplot.R
\name{gg.data.frame}
\alias{gg.data.frame}
\alias{gg.bru_prediction}
\alias{gg.prediction}
\alias{plot.bru_prediction}
\alias{plot.prediction}
\title{Geom for predictions}
\usage{
\method{gg}{data.frame}(...)

\method{gg}{bru_prediction}(data, mapping = NULL, ribbon = TRUE, alpha = NULL, bar = FALSE, ...)

\method{gg}{prediction}(data, ...)

\method{plot}{bru_prediction}(x, y = NULL, ...)

\method{plot}{prediction}(x, y = NULL, ...)
}
\arguments{
\item{\dots}{Arguments passed on to \code{geom_line}.}

\item{data}{A prediction object, usually the result of a \code{\link[=predict.bru]{predict.bru()}}
call.}

\item{mapping}{a set of aesthetic mappings created by \code{aes}. These are passed
on to \code{geom_line()}.
If "fill" is present, it is passed to \code{geom_ribbon()}.}

\item{ribbon}{If TRUE, plot a ribbon around the line based on the smallest
and largest quantiles present in the data, found by matching names starting
with \code{q} and followed by a numerical value.  \code{inla()}-style
\code{numeric+"quant"} names are converted to inlabru style before matching.}

\item{alpha}{The ribbons numeric alpha (transparency) level in \verb{[0,1]}.}

\item{bar}{If TRUE plot boxplot-style summary for each variable.}

\item{x}{a prediction object.}

\item{y}{Ignored argument but required for S3 compatibility.}
}
\value{
Concatenation of a \code{geom_line} value and optionally a \code{geom_ribbon}
value.
}
\description{
This geom serves to visualize \code{prediction} objects which usually results from
a call to \code{\link[=predict.bru]{predict.bru()}}. Predictions objects provide summary statistics
(mean, median, sd, ...) for one or more random variables. For single
variables (or if requested so by setting \code{bar = TRUE}), a boxplot-style geom
is constructed to show the statistics. For multivariate predictions the mean
of each variable (y-axis) is plotted against the row number of the variable
in the prediction data frame (x-axis) using \code{geom_line}. In addition, a
\code{geom_ribbon} is used to show the confidence interval.

Note: \code{gg.bru_prediction} also understands the format of INLA-style posterior
summaries, e.g. \code{fit$summary.fixed} for an inla object \code{fit}

Requires the \code{ggplot2} package.
}
\section{Functions}{
\itemize{
\item \code{gg(data.frame)}: This geom constructor will simply call \code{\link[=gg.bru_prediction]{gg.bru_prediction()}} for the data
provided.

\item \code{plot(bru_prediction)}: Generates a base ggplot2 using \code{ggplot()} and adds a geom for input \code{x} using
\code{\link[=gg.bru_prediction]{gg.bru_prediction()}}. Returns a ggplot object.

\item \code{plot(prediction)}: Identical to \code{\link[=gg.bru_prediction]{gg.bru_prediction()}}.

}}
\examples{
\donttest{
if (bru_safe_inla() &&
  requireNamespace("sn", quietly = TRUE) &&
  require("ggplot2", quietly = TRUE) &&
  require("patchwork", quietly = TRUE)) {
  # Generate some data

  input.df <- data.frame(x = cos(1:10))
  input.df <- within(
    input.df,
    {
      y <- 5 + 2 * cos(1:10) + rnorm(10, mean = 0, sd = 0.1)
    }
  )

  # Fit a model with fixed effect 'x' and intercept 'Intercept'

  fit <- bru(y ~ x, family = "gaussian", data = input.df)

  # Predict posterior statistics of 'x'

  xpost <- predict(fit, NULL, formula = ~x_latent)

  # The statistics include mean, standard deviation, the 2.5\% quantile, the
  # median, the 97.5\% quantile, minimum and maximum sample drawn from the
  # posterior as well as the coefficient of variation and the variance.

  xpost

  # For a single variable like 'x' the default plotting method invoked by
  # gg() will show these statistics in a fashion similar to a box plot:
  ggplot() +
    gg(xpost)


  # The predict function can also be used to simultaneously estimate
  # posteriors of multiple variables:

  xipost <- predict(fit,
    newdata = NULL,
    formula = ~ c(
      Intercept = Intercept_latent,
      x = x_latent
    )
  )
  xipost

  # If we still want a plot in the previous style we have to set the bar
  # parameter to TRUE

  p1 <- ggplot() +
    gg(xipost, bar = TRUE)
  p1

  # Note that gg also understands the posterior estimates generated while
  # running INLA

  p2 <- ggplot() +
    gg(fit$summary.fixed, bar = TRUE)
  (p1 / p2)

  # By default, if the prediction has more than one row, gg will plot the
  # column mean' against the row index. This is for instance useful for
  # predicting and plotting function but not very meaningful given the above
  # example:

  ggplot() +
    gg(xipost)

  # For ease of use we can also type

  plot(xipost)

  # This type of plot will show a ribbon around the mean, which visualizes
  # the upper and lower quantiles mentioned above (2.5\% and 97.5\%).
  # Plotting the ribbon can be turned of using the \code{ribbon} parameter

  ggplot() +
    gg(xipost, ribbon = FALSE)

  # Much like the other geomes produced by gg we can adjust the plot using
  # ggplot2 style commands, for instance

  ggplot() +
    gg(xipost) +
    gg(xipost, mapping = aes(y = median), ribbon = FALSE, color = "red")
}
}

}
\seealso{
Other geomes: 
\code{\link{gg}()},
\code{\link{gg.RasterLayer}()},
\code{\link{gg.SpatRaster}()},
\code{\link{gg.Spatial}},
\code{\link{gg.fm_mesh_1d}()},
\code{\link{gg.fm_mesh_2d}()},
\code{\link{gg.matrix}()},
\code{\link{gg.sf}()}
}
\concept{geomes}
