axis_unscale: Draw an axis with units on original scale

View source: R/plotting_support.R

axis_unscaleR Documentation

Draw an axis with units on original scale

Description

When plotting a standardized or rescaled variable, this function draws the axis units on the original scale.

Usage

axis_unscale( side = 1, at, orig, factor, ... )

Arguments

side

Side for axis. 1 is bottom.

at

Locations of tick marks, in original scale values

orig

The variable on original scale. Use this when variable was standardized.

factor

Factor the original variable was multiplied by to get rescaled variable. Use this when rescaling by a reference value, for example dividing by maximum value.

Details

This function draws a plot axis with display units on original scale. The typical situation for using this is when an analysis was performed on a standardized or rescaled variable. Plotting the posterior predictions with units on the transformed scale can make interpretation difficult.

When the variable was standardized (mean subtracted and divided by standard devation) before analysis, use the orig argument to point to the variable on the original scale.

When the variable was rescaled (multiplied by a factor to rescale, without relocating zero) before analysis, use the factor argument.

Author(s)

Richard McElreath

Examples

sppnames <- c( "afarensis","africanus","habilis","boisei",
    "rudolfensis","ergaster","sapiens")
brainvolcc <- c( 438 , 452 , 612, 521, 752, 871, 1350 )
masskg <- c( 37.0 , 35.5 , 34.5 , 41.5 , 55.5 , 61.0 , 53.5 )
d <- data.frame( species=sppnames , brain=brainvolcc , mass=masskg )
d$mass_std <- (d$mass - mean(d$mass))/sd(d$mass)
d$brain_std <- d$brain / max(d$brain)

m7.2 <- quap(
  alist(
      brain_std ~ dnorm( mu , exp(log_sigma) ),
      mu <- a + b[1]*mass_std + b[2]*mass_std^2,
      a ~ dnorm( 0.5 , 1 ),
      b ~ dnorm( 0 , 10 ),
      log_sigma ~ dnorm( 0 , 1 )
  ), data=d , start=list(b=rep(0,2)) )

plot( d$brain_std ~ d$mass_std , xaxt="n" , yaxt="n" , xlab="body mass (kg)" , ylab="brain volume (cc)" , col=rangi2 , pch=16 )
axis_unscale( 1 , at=quantile(d$mass) , d$mass )
axis_unscale( 2 , at=quantile(d$brain) , factor=max(d$brain) )

mass_seq <- seq(from=-1,to=1.5,length.out=30)
mu <- link(m7.2,data=list(mass_std=mass_seq))
mu <- apply(mu,2,mean)
lines( mass_seq , mu )


rmcelreath/rethinking documentation built on Aug. 26, 2024, 5:54 p.m.