R/ggp.logx.distn.R

Defines functions ggp.logx.distn

#' Make a plot of the diameter distribution using ggplot
#'
#' @param binned  A dataframe generated by make.log.bin.df
#' @param m.title A short title for the plot
#' @param x.units Integer: -3 (mm), -6 (um), -9 (nm), Default = -6.
#' @param d.max The maximum density for the plot. Default = -1 (calculate)
#' @param limits A list of min and max ECD. Default is c(5,500)
#' @param breaks Major diameter breaks for plot. Default is c(5, 10, 50, 100, 500)
#' @param minor_breaks Minor breaks for the plot. Defaults is c(6,7,8,9,20,30,40,60,70,80,90,200,300,400)
#'
#' @import ggplot2
#'
#' @examples
#'
#' library(rAnaLab)
#' data(diam)
#' l.b <- rb.lognormal.bin(diam[,1], n.root.2=8)
#' print(head(l.b))
#' binned <- make.log.bin.df(l.b)
#' print(head(binned))
#'
#' plt <- ggp.logx.distn(binned,
#'                       "AgX Grain Diameter",
#'                       x.units=-9,
#'                       d.max=0.025,
#'                       limits=c(50, 500),
#'                       breaks=c(50, 100, 500),
#'                       minor_breaks=c( 60, 70, 80, 90, 200, 300, 400))
#'
#' print(plt)
#'
#' @export
#'
ggp.logx.distn <- function(binned,
                           m.title,
                           x.units=-6,
                           d.max=-1,
                           limits=c(5,500),
                           breaks=c(5, 10, 50, 100, 500),
                           minor_breaks=c(6,7,8,9,20,30,40,60,70,80,90,200,300,400))
{

  md <- max(binned$dens)
  nP <- sum(binned$cts)

  if(x.units == -3) {
    x.lab = "ECD [mm]"
  } else if(x.units == -6){
    x.lab = "ECD [\U00B5m]"
  } else {
    x.lab = "ECD [nm]"
  }

  if (d.max < 0){
    d.max <- 1.1*md
  }

  plotECD <- ggplot() +
    geom_point(data=binned, aes(x=diam, y=dens),
               colour="darkblue") +
    scale_x_log10(limits=limits, breaks=breaks, minor_breaks=minor_breaks) +
    ylim(0, d.max) +
    xlab(x.lab) +
    ylab("Density") +
    ggtitle(sprintf("%s (n=%d)", m.title, nP)) +
    theme(axis.text=element_text(size=12),
          axis.title=element_text(size=12),
          plot.title=element_text(hjust = 0.5)) # center the title
  plotECD
}
jrminter/rAnaLab documentation built on July 20, 2020, 4:09 a.m.