File: multhist.R

package info (click to toggle)
r-cran-plotrix 3.8-4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,588 kB
  • sloc: makefile: 6
file content (32 lines) | stat: -rwxr-xr-x 1,169 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
multhist <- function (x, beside=TRUE, freq=NULL, probability=!freq,
 plot.it=TRUE, ...) {
 
 ## sort out histogram arguments
 hist.args <- formals(hist.default)
 args <- list(...)
 hargs <- names(args)[names(args) %in% names(hist.args)]
 hist.args[hargs] <- args[hargs]
 hist.args$plot<-FALSE
 allhist <- hist(unlist(x),hist.args$breaks,plot=FALSE)
 ## sort out barplot arguments
 if(plot.it) {
  barplot.args <- formals(barplot.default)
  bargs <- names(args)[names(args) %in% names(barplot.args)]
  barplot.args[bargs] <- args[bargs]
  barplot.args$beside <- beside
  if("ann" %in% names(barplot.args))
   barplot.args$ann<-eval(barplot.args$ann,envir=barplot.args)
  ## prevent warnings
  barplot.args$"..." <- barplot.args$inside <- NULL
  if (!"names.arg" %in% bargs)
   barplot.args$names.arg <- signif(allhist$mids, 2)
  if (is.null(freq)) {
   freq<-if(!missing(probability)) !as.logical(probability) else TRUE
  }
  comp <- if(freq) "counts" else comp<-"density"
  combhist <-
   t(sapply(x,function(z) hist(z,breaks=allhist$breaks,plot=FALSE)[[comp]]))
  if(plot.it) do.call("barplot", c(list(combhist),barplot.args))
  invisible(list(allhist,combhist))
 }
}