stat_plotlm: Add a trend line and SE ribbon for a model fit object

View source: R/stat_plotlm_v3.R View source: R/stat_plotlm_v2.R View source: R/stat_plotlm.R

stat_plotlmR Documentation

Add a trend line and SE ribbon for a model fit object

Description

This is a replacement for stat_smooth where you can supply your own model fit (lm or lmer) to be plotted as a trend line.

This is a replacement for stat_smooth where you can supply your own model fit (lm or lmer) to be plotted as a trend line.

This is a replacement for stat_smooth where you can supply your own model fit (lm or lmer) to be plotted as a trend line.

Usage

stat_plotlm(
  mapping = NULL,
  data = NULL,
  geom = "smooth",
  position = "identity",
  show.legend = NA,
  inherit.aes = TRUE,
  fitted_model = NULL,
  se = TRUE,
  fullrange = FALSE,
  n = 100,
  SEs = 2,
  fulldata = NULL,
  ...
)

stat_plotlm(
  mapping = NULL,
  data = NULL,
  geom = "smooth",
  position = "identity",
  show.legend = NA,
  inherit.aes = TRUE,
  fitted_model = NULL,
  se = TRUE,
  fullrange = FALSE,
  n = 100,
  SEs = 2,
  fulldata = NULL,
  ...
)

stat_plotlm(
  mapping = NULL,
  data = NULL,
  geom = "smooth",
  position = "identity",
  show.legend = NA,
  inherit.aes = TRUE,
  fitted_model = NULL,
  se = TRUE,
  fullrange = FALSE,
  n = 100,
  SEs = 2,
  fulldata = NULL,
  ...
)

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)).

geom

Use to override the default connection between geom_smooth() and stat_smooth().

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

fitted_model

a model object (ex from lm or lmer) compatible with the 'predict' function

se

display confidence interval around smooth? (TRUE by default, see level to control)

fullrange

Should the fit span the full range of the plot, or just the data?

n

Number of points at which to evaluate smoother.

SEs

width of confidence interval band (in se's)

...

Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat.

Details

The main difference from stat_smooth, or other geom_ of stat_ functions is that you must add all of the terms in the model as Aesthetics in the aes() argument, naming them the same as in your data: aes(Group1 = Group1, Group2 = Group2, Covariate = Covariate)

The main difference from stat_smooth, or other geom_ of stat_ functions is that you must add all of the terms in the model as Aesthetics in the aes() argument, naming them the same as in your data: aes(Group1 = Group1, Group2 = Group2, Covariate = Covariate)

The main difference from stat_smooth, or other geom_ of stat_ functions is that you must add all of the terms in the model as Aesthetics in the aes() argument, naming them the same as in your data: aes(Group1 = Group1, Group2 = Group2, Covariate = Covariate)

Examples

library(ggplot2)
lm_mpg = lm(hwy ~ displ,mpg)
ggplot(mpg,aes(x=displ,y=hwy)) + geom_point() +
    stat_plotlm(fitted_model = lm_mpg,aes(displ = displ),se=T)

# add faceting
lm_drv = lm(hwy ~ poly(displ,2)*drv,mpg)
ggplot(mpg,aes(x=displ,y=hwy)) + geom_point() + facet_wrap(~drv,scales = 'free') +
    stat_plotlm(fitted_model = lm_drv,aes(displ = displ,drv=drv),se=T,fullrange=T,n=20)

# add multiple colors in each facet. Note that the group aesthetic is necessary for stat_plotlm
lm_trans = lm(hwy ~ poly(displ,2)*drv + trans,mpg)
ggplot(mpg,aes(x=displ,y=hwy)) + geom_point(aes(color = drv)) + facet_wrap(~trans,scales = 'free') +
   stat_plotlm(fitted_model = lm_trans,
               aes(group = drv,color = drv,displ = displ,drv=drv,trans=trans),
               se=T,fullrange=T,n=20)
library(ggplot2)
lm_mpg = lm(hwy ~ displ,mpg)
ggplot(mpg,aes(x=displ,y=hwy)) + geom_point() +
    stat_plotlm(fitted_model = lm_mpg,aes(displ = displ),se=T)

# add faceting
lm_drv = lm(hwy ~ poly(displ,2)*drv,mpg)
ggplot(mpg,aes(x=displ,y=hwy)) + geom_point() + facet_wrap(~drv,scales = 'free') +
    stat_plotlm(fitted_model = lm_drv,aes(displ = displ,drv=drv),se=T,fullrange=T,n=20)

# add multiple colors in each facet. Note that the group aesthetic is necessary for stat_plotlm
lm_trans = lm(hwy ~ poly(displ,2)*drv + trans,mpg)
ggplot(mpg,aes(x=displ,y=hwy)) + geom_point(aes(color = drv)) + facet_wrap(~trans,scales = 'free') +
   stat_plotlm(fitted_model = lm_trans,
               aes(group = drv,color = drv,displ = displ,drv=drv,trans=trans),
               se=T,fullrange=T,n=20)
library(ggplot2)
lm_mpg = lm(hwy ~ displ,mpg)
ggplot(mpg,aes(x=displ,y=hwy)) + geom_point() +
    stat_plotlm(fitted_model = lm_mpg,aes(displ = displ),se=T)

# add faceting
lm_drv = lm(hwy ~ poly(displ,2)*drv,mpg)
ggplot(mpg,aes(x=displ,y=hwy)) + geom_point() + facet_wrap(~drv,scales = 'free') +
    stat_plotlm(fitted_model = lm_drv,aes(displ = displ,drv=drv),se=T,fullrange=T,n=20)

# add multiple colors in each facet. Note that the group aesthetic is necessary for stat_plotlm
lm_trans = lm(hwy ~ poly(displ,2)*drv + trans,mpg)
ggplot(mpg,aes(x=displ,y=hwy)) + geom_point(aes(color = drv)) + facet_wrap(~trans,scales = 'free') +
   stat_plotlm(fitted_model = lm_trans,
               aes(group = drv,color = drv,displ = displ,drv=drv,trans=trans),
               se=T,fullrange=T,n=20)

deruncie/PLS205_package documentation built on March 25, 2022, 2:29 a.m.