Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

histStack

Histogram "stacked" by categories


Description

Histogram of a quantitative variable with bars that are "stacked" by the values of a factor variable.

Usage

histStack(x,...)

 ## S3 method for class 'formula'
histStack(x,data,breaks="Sturges",col="rainbow",
  right=TRUE,main="",xlab=NULL,legend.pos=NULL,cex.legend=0.75,...)

 ## Default S3 method:
histStack(x,z,breaks="Sturges",col="rainbow",
  right=TRUE,main="",xlab=NULL,legend.pos=NULL,cex.legend=0.75,...)

Arguments

x

A vector of quantitative data or a formula of the form x~z (see z below).

z

A vector of categorical data (a factor) that will define the “stacks”.

data

A data frame that contains both x and z.

breaks

Breaks to use in categorizing values of x.

col

Either a vector of colors in any legitimate form or a character string that specifies a function that requires only the length of the vector as an argument and will return a vector of colors with that length. (see Details)

right

A logical that indicates whether the bins are right-open (left-closed; =TRUE) or right-closed (left-open; =FALSE; default).

main

A character string that forms the main title for the plot.

xlab

A character string for labeling the x-axis.

legend.pos

A character string or two numeric values indicating the position for the stacking legend.

cex.legend

A numeric character expansion value for the legend. Values less than 1 will make the legend smaller.

...

Additional arguments sent to the hist function.

Details

histStack displays a “stacked histogram” while using many of the same arguments as hist(). The argument z will be converted to a factor with a warning if it is not already a factor.

The color functions in grDevices (e.g. "gray.colors") should always be valid when passed as the col argument. Any function that will return a vector of n colors when called with a single argument n and that exists in the current environment should work. An error will occur if length(col)==1 and the value is not a function as described for col (e.g., col="blue" will result in an error).If fewer colors than levels of z are passed, they will be recycled.

Value

nil. A plot is displayed.

Note

This function is currently experimental.

Author(s)

Derek Ogle with modifications by Jim Lemon

See Also

Examples

set.seed(409)
 df<-data.frame(len=rnorm(100)+5,
  grp=sample(c("A","B","C","D"),100,replace=TRUE))
 histStack(len~grp,data=df,main="Default (rainbow) colors",
  xlab="Length category")
 histStack(len~grp,data=df,col="heat.colors",main="Heat colors",
  xlab="Length category",legend.pos="topright")
 histStack(len~grp,data=df,col=2:5,main="Colors by number",
  xlab="Length category",legend.pos=c(2.8,18))

plotrix

Various Plotting Functions

v3.8-1
GPL (>= 2)
Authors
Jim Lemon, Ben Bolker, Sander Oom, Eduardo Klein, Barry Rowlingson, Hadley Wickham, Anupam Tyagi, Olivier Eterradossi, Gabor Grothendieck, Michael Toews, John Kane, Rolf Turner, Carl Witthoft, Julian Stander, Thomas Petzoldt, Remko Duursma, Elisa Biancotto, Ofir Levy, Christophe Dutang, Peter Solymos, Robby Engelmann, Michael Hecker, Felix Steinbeck, Hans Borchers, Henrik Singmann, Ted Toal, Derek Ogle, Darshan Baral, Ulrike Groemping, Bill Venables
Initial release
2021-01-21

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.