R/scatterhist.R

Defines functions scatterhist

Documented in scatterhist

#' Creating a scatter plot and histogram combo
#'
#' This functions reads in to vectors of the same size and plots them on a scatter plot. It also creates a histogram on each axis of the scatter plot.
#'
#' @param x a vector of values
#' @param y a vector of values
#' @param xlab a label for the x axis
#' @param ylab a label for the y axis
#'
#' @return Scatter plot with frequency of points on each axis
#' @export
#'
#' @examples x = c(1,2,3,3,5); y = c(1,2,3,4,4); scatterhist(x, y, xlab = "x-axis", ylab = "y-axis")
scatterhist = function(x, y, xlab="", ylab=""){
  zones=matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
  layout(zones, widths=c(4/5,1/5), heights=c(1/5,4/5))
  xhist = hist(x, plot=FALSE)
  yhist = hist(y, plot=FALSE)
  top = max(c(xhist$counts, yhist$counts))
  par(mar=c(3,3,1,1))
  plot(x,y)
  par(mar=c(0,3,1,1))
  barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)
  par(mar=c(3,0,1,1))
  barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)
  par(oma=c(3,3,0,0))
  mtext(xlab, side=1, line=1, outer=TRUE, adj=0,
        at=.8 * (mean(x) - min(x))/(max(x)-min(x)))
  mtext(ylab, side=2, line=1, outer=TRUE, adj=0,
        at=(.8 * (mean(y) - min(y))/(max(y) - min(y))))
}
sche97/MATH4753 documentation built on April 17, 2020, 9:39 a.m.