R/ggplot2_histPosteriorcvfNB.R

histPosteriorcvfNB <- function(DT, scaling, matNameFeature, binWidth){
    ## Error checking
    if(!(scaling %in% c("linear", "log10"))){
        stop("scaling options: 'linear', 'log10'")
    }

    if(exists("binWidth") == F){
        stop("Must specify a binWidth")
    }

    if(scaling == "linear"){
        g <- ggplot(data = DT, aes(x = as.numeric(posterior),
                                   fill=as.factor(Actual_choice))) +
            geom_histogram(colour = "black",
                           alpha = 0.75,
                           position = position_stack(reverse = TRUE),
                           binwidth = binWidth/10000) +
            xlab("Posterior Probability") +
            theme_bw() +
            scale_fill_manual(name="Actual Choice", values=c("#F8766D", "#00BFC4")) +
            ggtitle(paste(matNameFeature, collapse = "_")) +
            guides(fill = guide_legend(reverse = TRUE))
    } else if(scaling == "log10"){
        g <- ggplot(data = DT, aes(x = log10(as.numeric(posterior)),
                                   fill=as.factor(Actual_choice))) +
            geom_histogram(colour = "black",
                           alpha = 0.75,
                           position = position_stack(reverse = TRUE),
                           binwidth = binWidth) +
            xlab("log10(Posterior Probability)") +
            theme_bw() +
            scale_fill_manual(name="Actual Choice", values=c("#F8766D", "#00BFC4")) +
            ggtitle(paste(matNameFeature, collapse = "_")) +
            guides(fill = guide_legend(reverse = TRUE)) +
            scale_x_continuous(limits = c(-20, NA))
    }

    return(g)
}
jchitpin/blistR documentation built on July 8, 2019, 6:29 p.m.