├── inst └── .lintr ├── .github ├── .gitignore └── workflows │ ├── pkgdown.yaml │ └── check-standard.yaml ├── tests ├── figs │ ├── deps.txt │ ├── boxplot │ │ ├── boxplot-horiz.svg │ │ ├── boxplot-basic.svg │ │ └── boxplot-labels.svg │ ├── barplot │ │ ├── barplot-basic-no-axisnames.svg │ │ ├── barplot-basic.svg │ │ ├── barplot-horiz.svg │ │ └── barplot-labels.svg │ ├── plotting │ │ ├── plot-x-xlab-ylab.svg │ │ ├── plot-x-xlab.svg │ │ ├── plot-x-ylab.svg │ │ ├── plot-x.svg │ │ ├── plot-x-y-xlab-ylab.svg │ │ ├── plot-x-y-xlab.svg │ │ ├── plot-x-y-ylab.svg │ │ ├── plot-x-y.svg │ │ ├── plot-x-y-mat-single.svg │ │ ├── plot-x-y-mat.svg │ │ └── plot-x-y-mat-single-vector.svg │ └── histograms │ │ ├── hist-basic.svg │ │ └── hist-density.svg ├── testthat.R └── testthat │ ├── helper-utils.R │ ├── test_s3_plot.R │ ├── test_hist.R │ ├── test_boxplot.R │ ├── test_barplot.R │ └── test_plot.R ├── man ├── figures │ ├── README-qqplots-1.png │ ├── README-plot-expand-1.png │ ├── README-plot-minimal-1.png │ ├── README-unnamed-chunk-6-1.png │ ├── README-unnamed-chunk-7-1.png │ └── README-plot-minimal-full-1.png ├── reset_prettyB.Rd ├── prettyB-package.Rd ├── add_y_axis.Rd ├── boxplot.prettyB.Rd ├── plot.prettyB.Rd ├── hist.prettyB.Rd └── barplot.prettyB.Rd ├── .Rbuildignore ├── .lintr ├── codecov.yml ├── cran-comments.md ├── prettyB.Rproj ├── R ├── prettyB-package.R ├── boxplot_helper.R ├── zzz.R ├── par.R ├── qqnorm_minimal.R ├── ticks_title.R ├── hist.R ├── plot.R ├── barplot.R └── boxplot.R ├── .gitignore ├── NAMESPACE ├── DESCRIPTION ├── NEWS.md ├── vignettes └── introduction.Rmd ├── README.Rmd └── README.md /inst/.lintr: -------------------------------------------------------------------------------- 1 | ../.lintr -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /tests/figs/deps.txt: -------------------------------------------------------------------------------- 1 | - vdiffr-svg-engine: 1.0 2 | - vdiffr: 0.3.3 3 | - freetypeharfbuzz: 0.2.6 4 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library("testthat") 2 | 3 | test_check("prettyB") 4 | 5 | # To rerun tests 6 | # manage_cases() 7 | -------------------------------------------------------------------------------- /man/figures/README-qqplots-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpingrivers/prettyB/HEAD/man/figures/README-qqplots-1.png -------------------------------------------------------------------------------- /man/figures/README-plot-expand-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpingrivers/prettyB/HEAD/man/figures/README-plot-expand-1.png -------------------------------------------------------------------------------- /man/figures/README-plot-minimal-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpingrivers/prettyB/HEAD/man/figures/README-plot-minimal-1.png -------------------------------------------------------------------------------- /man/figures/README-unnamed-chunk-6-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpingrivers/prettyB/HEAD/man/figures/README-unnamed-chunk-6-1.png -------------------------------------------------------------------------------- /man/figures/README-unnamed-chunk-7-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpingrivers/prettyB/HEAD/man/figures/README-unnamed-chunk-7-1.png -------------------------------------------------------------------------------- /man/figures/README-plot-minimal-full-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpingrivers/prettyB/HEAD/man/figures/README-plot-minimal-full-1.png -------------------------------------------------------------------------------- /tests/testthat/helper-utils.R: -------------------------------------------------------------------------------- 1 | expect_doppelganger = function(title, fig, path = NULL, ...) { 2 | 3 | testthat::skip_if_not_installed("vdiffr") 4 | vdiffr::expect_doppelganger(title, fig, path = path, ...) 5 | } 6 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^\.travis\.yml$ 2 | ^codecov\.yml$ 3 | ^.*\.Rproj$ 4 | ^\.Rproj\.user$ 5 | ^README\.Rmd$ 6 | Rplots\.pdf 7 | ^\.travis\.yml$ 8 | ^cran-comments\.md$ 9 | ^\.lintr$ 10 | ^inst/\.lintr$ 11 | ^\.github$ 12 | -------------------------------------------------------------------------------- /.lintr: -------------------------------------------------------------------------------- 1 | linters: with_defaults( 2 | assignment_linter = NULL, # 20 3 | commented_code_linter = NULL, 4 | object_name_linter = NULL, 5 | line_length_linter(100), 6 | cyclocomp_linter = NULL) 7 | comment_bot: FALSE 8 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | 3 | coverage: 4 | status: 5 | project: 6 | default: 7 | target: auto 8 | threshold: 1% 9 | patch: 10 | default: 11 | target: auto 12 | threshold: 1% 13 | -------------------------------------------------------------------------------- /tests/testthat/test_s3_plot.R: -------------------------------------------------------------------------------- 1 | test_that("Testing S3 plot functions", { 2 | context("Plot only x") 3 | testthat::skip_on_ci() 4 | testthat::skip_on_cran() 5 | expect_null(plot(iris$Sepal.Length ~ iris$Sepal.Width)) 6 | expect_null({ 7 | m = lm(1:10 ~ rnorm(10)) 8 | plot(m, which = 1:5, sub.caption = NA, ask = FALSE) 9 | }) 10 | } 11 | ) 12 | -------------------------------------------------------------------------------- /man/reset_prettyB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/par.R 3 | \name{reset_prettyB} 4 | \alias{reset_prettyB} 5 | \alias{setup_prettyB} 6 | \title{Set up par and palette 7 | 8 | Sets up par and palette} 9 | \usage{ 10 | reset_prettyB() 11 | 12 | setup_prettyB() 13 | } 14 | \description{ 15 | Set up par and palette 16 | 17 | Sets up par and palette 18 | } 19 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Test environments 2 | - local x86_64-pc-linux-gnu install, R 3.6.0 3 | - R-hub ubuntu-gcc-release (r-release) 4 | - R-hub windows-x86_64-devel (r-devel) 5 | - R-hub fedora-clang-devel (r-devel) 6 | - win-builder (all versions) 7 | - travis-ci (all versions) 8 | 9 | ## R CMD check results 10 | 11 | 0 errors | 0 warnings | 1 note 12 | 13 | * Note: Previous version called par() onload. This have been removed. 14 | -------------------------------------------------------------------------------- /tests/testthat/test_hist.R: -------------------------------------------------------------------------------- 1 | test_that("Testing hist", { 2 | context("histograms") 3 | testthat::skip_on_ci() 4 | testthat::skip_on_cran() 5 | set.seed(1) 6 | x = rlnorm(100) 7 | expect_doppelganger("hist basic", hist_p(x)) 8 | expect_doppelganger("hist density", hist_p(x, freq = FALSE)) 9 | expect_doppelganger("hist labels", 10 | hist_p(x, xlab = "X", ylab = "Y", 11 | main = "Main", sub = "Sub")) 12 | 13 | }) 14 | -------------------------------------------------------------------------------- /tests/testthat/test_boxplot.R: -------------------------------------------------------------------------------- 1 | test_that("Testing boxplot", { 2 | context("Boxplot") 3 | testthat::skip_on_ci() 4 | testthat::skip_on_cran() 5 | set.seed(1) 6 | x = rlnorm(100) 7 | expect_doppelganger("Boxplot basic", boxplot_p(x)) 8 | expect_doppelganger("Boxplot horiz", boxplot_p(x, horizontal = TRUE)) 9 | expect_doppelganger("Boxplot labels", 10 | boxplot_p(x, xlab = "X", ylab = "Y", 11 | main = "Main", sub = "Sub")) 12 | }) 13 | -------------------------------------------------------------------------------- /prettyB.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageCheckArgs: --as-cran 22 | PackageRoxygenize: rd,collate,namespace 23 | -------------------------------------------------------------------------------- /R/prettyB-package.R: -------------------------------------------------------------------------------- 1 | #' The prettyB package 2 | #' 3 | #' Anyone who uses R Base graphics, have a 100 and 1 tweaks that they use to make the 4 | #' figures more presentable. This package aims to capture the tweaks in one place. 5 | #' By masking_standard plotting functions, we can automatically make base graphics a bit 6 | #' more pretty. 7 | #' @name prettyB-package 8 | #' @aliases prettyB prettyb prettyB-package 9 | #' @docType package 10 | #' @author \email{csgillespie@gmail.com} 11 | #' @keywords package 12 | #' @seealso \url{https://github.com/jumpingrivers/prettyB} 13 | NULL 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # History files 2 | .Rhistory 3 | .Rapp.history 4 | # Session Data files 5 | .RData 6 | # Example code in package build process 7 | *-Ex.R 8 | # Output files from R CMD build 9 | /*.tar.gz 10 | # Output files from R CMD check 11 | /*.Rcheck/ 12 | # RStudio files 13 | .Rproj.user/ 14 | # produced vignettes 15 | vignettes/*.html 16 | vignettes/*.pdf 17 | # OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 18 | .httr-oauth 19 | # knitr and R markdown default cache directories 20 | /*_cache/ 21 | /cache/ 22 | # Temporary files created by R markdown 23 | *.utf8.md 24 | *.knit.md 25 | .Rproj.user 26 | Rplots.pdf 27 | -------------------------------------------------------------------------------- /tests/testthat/test_barplot.R: -------------------------------------------------------------------------------- 1 | test_that("Testing barplot", { 2 | context("Barplot") 3 | testthat::skip_on_travis() 4 | testthat::skip_on_ci() 5 | testthat::skip_on_cran() 6 | expect_doppelganger("barplot basic", 7 | barplot_p(VADeaths)) 8 | expect_doppelganger("barplot horiz", 9 | barplot_p(VADeaths, horiz = TRUE)) 10 | expect_doppelganger("barplot labels", 11 | barplot_p(VADeaths, xlab = "X", ylab = "Y", 12 | main = "Main", sub = "Sub")) 13 | 14 | expect_doppelganger("barplot basic no axisnames", 15 | barplot_p(VADeaths, axisnames = FALSE)) 16 | }) 17 | -------------------------------------------------------------------------------- /man/prettyB-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/prettyB-package.R 3 | \docType{package} 4 | \name{prettyB-package} 5 | \alias{prettyB-package} 6 | \alias{prettyB} 7 | \alias{prettyb} 8 | \title{The prettyB package} 9 | \description{ 10 | Anyone who uses R Base graphics, have a 100 and 1 tweaks that they use to make the 11 | figures more presentable. This package aims to capture the tweaks in one place. 12 | By masking_standard plotting functions, we can automatically make base graphics a bit 13 | more pretty. 14 | } 15 | \seealso{ 16 | \url{https://github.com/jumpingrivers/prettyB} 17 | } 18 | \author{ 19 | \email{csgillespie@gmail.com} 20 | } 21 | \keyword{package} 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(barplot,prettyB) 4 | S3method(boxplot,prettyB) 5 | S3method(hist,prettyB) 6 | S3method(plot,prettyB) 7 | export(add_x_axis) 8 | export(add_y_axis) 9 | export(barplot_p) 10 | export(boxplot_p) 11 | export(hist_p) 12 | export(plot_p) 13 | export(reset_prettyB) 14 | export(setup_prettyB) 15 | importFrom(grDevices,palette) 16 | importFrom(grDevices,rgb) 17 | importFrom(grDevices,xy.coords) 18 | importFrom(graphics,abline) 19 | importFrom(graphics,axTicks) 20 | importFrom(graphics,axis) 21 | importFrom(graphics,barplot) 22 | importFrom(graphics,barplot.default) 23 | importFrom(graphics,boxplot) 24 | importFrom(graphics,grid) 25 | importFrom(graphics,par) 26 | importFrom(graphics,plot.default) 27 | importFrom(graphics,title) 28 | -------------------------------------------------------------------------------- /man/add_y_axis.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ticks_title.R 3 | \name{add_x_axis} 4 | \alias{add_x_axis} 5 | \alias{add_y_axis} 6 | \title{Add tick marks to x/y axis 7 | 8 | A nicer set of default tick marks} 9 | \usage{ 10 | add_x_axis(ticks_x, labels = ticks_x, tick = TRUE, lwd = 0, lwd.ticks = 1) 11 | 12 | add_y_axis(ticks_y, labels = ticks_y, tick = TRUE, lwd = 0, lwd.ticks = 1) 13 | } 14 | \arguments{ 15 | \item{ticks_x}{location of x tick mark} 16 | 17 | \item{labels}{tick label} 18 | 19 | \item{tick}{Display tick mark} 20 | 21 | \item{lwd}{width of line} 22 | 23 | \item{lwd.ticks}{width of tick mark} 24 | 25 | \item{ticks_y}{location of y tick mark} 26 | } 27 | \description{ 28 | Add tick marks to x/y axis 29 | 30 | A nicer set of default tick marks 31 | } 32 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Type: Package 2 | Package: prettyB 3 | Title: Pretty Base Graphics 4 | Version: 0.2.2 5 | Authors@R: 6 | person(given = "Colin", 7 | family = "Gillespie", 8 | role = c("aut", "cre"), 9 | email = "csgillespie@gmail.com") 10 | Maintainer: Colin Gillespie 11 | Description: Drop-in replacements for standard base graphics 12 | functions. The replacements are prettier versions of the originals. 13 | License: GPL-2 | GPL-3 14 | URL: https://github.com/jumpingrivers/prettyB/ 15 | BugReports: https://github.com/jumpingrivers/prettyB/issues 16 | Imports: 17 | graphics, 18 | grDevices, 19 | stats 20 | Suggests: 21 | covr, 22 | knitr, 23 | lintr, 24 | rmarkdown, 25 | testthat, 26 | vdiffr 27 | VignetteBuilder: 28 | knitr 29 | Encoding: UTF-8 30 | LazyData: true 31 | RoxygenNote: 7.1.1 32 | -------------------------------------------------------------------------------- /R/boxplot_helper.R: -------------------------------------------------------------------------------- 1 | get_name_axis = function(at, lim, boxwex, box_out, log) { 2 | box_padding = max(0, 1 - boxwex) / 2 3 | if (!is.null(at)) { 4 | ticks = at 5 | if (!is.null(lim)) { 6 | lim = c(1 - boxwex / 2 - box_padding, 7 | length(at) + boxwex / 2 + box_padding) 8 | } 9 | } else if (is.null(lim)) { 10 | ticks = seq_len(length(box_out$names)) 11 | lim = c(1 - boxwex / 2 - box_padding, 12 | length(box_out$names) + boxwex / 2 + box_padding) 13 | } else { 14 | ticks = pretty(lim) 15 | lim = range(ticks) 16 | } 17 | list(lim = lim, ticks = ticks) 18 | } 19 | 20 | get_number_axis = function(lim, box_out, log) { 21 | if (!is_y(log)) { 22 | if (is.null(lim)) { 23 | y_tmp = range(box_out$stats) 24 | ticks = pretty(y_tmp) 25 | lim = extend_axis(range(ticks)) 26 | } else { 27 | ticks = pretty(lim) 28 | lim = range(ticks) 29 | } 30 | } 31 | list(lim = lim, ticks = ticks) 32 | } 33 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | cache = new.env() 2 | current = new.env() 3 | 4 | prettyB_pal = function(alpha = 255) { 5 | c(rgb(85, 130, 169, alpha = alpha, maxColorValue = 255), 6 | rgb(200, 79, 178, alpha = alpha, maxColorValue = 255), 7 | rgb(105, 147, 45, alpha = alpha, maxColorValue = 255), 8 | rgb(204, 74, 83, alpha = alpha, maxColorValue = 255), 9 | rgb(183, 110, 39, alpha = alpha, maxColorValue = 255), 10 | rgb(131, 108, 192, alpha = alpha, maxColorValue = 255), 11 | rgb(63, 142, 96, alpha = alpha, maxColorValue = 255)) 12 | 13 | } 14 | 15 | #' @importFrom grDevices palette rgb 16 | .onLoad = function(libname, pkgname) { 17 | 18 | # Cache current values 19 | cache$palette = NULL 20 | cache$par = NULL 21 | 22 | cache$first = TRUE 23 | 24 | ## Set values 25 | #theme_set(theme = "minimal") 26 | } 27 | 28 | .onUnload = function(libpath) { 29 | # if (!is.null(cache$par)) { 30 | # par(cache$par) 31 | # } 32 | # 33 | # if (!is.null(cache$palette)) { 34 | # palette(cache$palette) 35 | # } 36 | } 37 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # prettyB 0.2.2 _2021-02-09_ 2 | * Export add_y_axis & add_x_axis tick functions 3 | * Improved `plot()` logic for the log scale 4 | * Conditionally use {vdiffr} for testing (CRAN request) 5 | * Bug: plot matrices fixes #13 (thanks to @gmonaie) 6 | * BUg: barplot and axisnames. Fixes #14 (thanks to @yikeshu0611) 7 | 8 | ## Version 0.2.1 9 | * Bug fix: Don't open plotting window when loading the package 10 | * Don't override default argument (CRAN request) 11 | * Implement `hist_p()` method 12 | * Implement `barplot_p()` method 13 | * Implement `boxplot_p()` method 14 | * Play nicely with existing S3 plotting methods, e.g. `plot.lm()` 15 | * KISS: removed themes. If you want themes, use {ggplot2} 16 | * Made grid lines solid, instead of dashed 17 | * Add vignette 18 | * Update test suite 19 | 20 | ## Version 0.1.1 21 | * Adding examples to `?plot` 22 | 23 | ## Version 0.1.0 24 | * Initial version 25 | 26 | ----- 27 | 28 | Development of this package was supported by [Jumping 29 | Rivers](https://www.jumpingrivers.com) 30 | -------------------------------------------------------------------------------- /R/par.R: -------------------------------------------------------------------------------- 1 | #' Set up par and palette 2 | #' 3 | #' Sets up par and palette 4 | #' @export 5 | reset_prettyB = function() { 6 | if (!is.null(cache$par)) { 7 | graphics::par(cache$par) 8 | cache$par = NULL 9 | } 10 | if (!is.null(cache$palette)) { 11 | palette(cache$palette) 12 | cache$palette = NULL 13 | } 14 | } 15 | 16 | #' @rdname reset_prettyB 17 | #' @export 18 | setup_prettyB = function() { 19 | new_par = list(mar = c(3, 3, 2, 1), mgp = c(2, 0.4, 0), tck = -.01, 20 | cex.axis = 0.9, las = 1, 21 | xaxs = "i", yaxs = "i", 22 | col.lab = "grey50", col.main = "grey20", 23 | pch = 21, col = 1, lwd = 1.5) 24 | new_par_names = names(new_par) 25 | cur_par = par(no.readonly = TRUE) 26 | if (is.null(cache$par)) { 27 | cache$par = cur_par 28 | } 29 | cached_par = cache$par 30 | 31 | if (is.null(cache$palette)) { 32 | cache$palette = palette() 33 | palette(prettyB_pal()) 34 | } 35 | changes = vapply(new_par_names, 36 | function(i) identical(cur_par[[i]], cached_par[[i]]), 37 | logical(1)) 38 | graphics::par(new_par[changes]) 39 | } 40 | -------------------------------------------------------------------------------- /tests/testthat/test_plot.R: -------------------------------------------------------------------------------- 1 | test_that("Testing plot", { 2 | context("Plotting") 3 | testthat::skip_on_ci() 4 | testthat::skip_on_cran() 5 | expect_doppelganger("Plot x", plot_p(1:10)) 6 | expect_doppelganger("Plot x-xlab", plot_p(1:10, xlab = "X")) 7 | expect_doppelganger("Plot x-ylab", plot_p(1:10, ylab = "Y")) 8 | expect_doppelganger("Plot x-xlab-ylab", 9 | plot_p(1:10, xlab = "X", ylab = "Y")) 10 | 11 | expect_doppelganger("Plot x-y-par-mfrow", { 12 | op = par(mfrow = c(1, 2)) 13 | plot_p(1:10, 11:20) 14 | plot_p(1:10, xlab = "X") 15 | par(op) 16 | } 17 | ) 18 | 19 | expect_doppelganger("Plot x-y", plot_p(1:10, 11:20)) 20 | expect_doppelganger("Plot x-y-xlab", plot_p(1:10, 11:20, xlab = "X")) 21 | expect_doppelganger("Plot x-y-ylab", plot_p(1:10, 11:20, ylab = "Y")) 22 | expect_doppelganger("Plot x-y-xlab-ylab", 23 | plot_p(1:10, 11:20, xlab = "X", ylab = "Y")) 24 | 25 | expect_doppelganger("Plot x-y-formula", 26 | plot(iris$Sepal.Length ~ iris$Sepal.Width) 27 | ) 28 | 29 | ## Testing matrix 30 | x = matrix(c(41:60, 11:30, 51:70), ncol = 3) 31 | expect_doppelganger("Plot x-y-mat", plot_p(x)) 32 | expect_doppelganger("Plot x-y-mat-single-vector", plot_p(x[, 1], 1:20)) 33 | expect_doppelganger("Plot x-y-mat-single", plot_p(x[, 1])) 34 | 35 | }) 36 | -------------------------------------------------------------------------------- /R/qqnorm_minimal.R: -------------------------------------------------------------------------------- 1 | #lty = 3, col =2, lwd = 2 2 | 3 | # qqnorm_minimal = function(y, ...) { 4 | # 5 | # ## Set up par 6 | # op = set_par_minimal() 7 | # on.exit(graphics::par(op)) 8 | # 9 | # new_args = list(pch = 21, bg = 1, axes = FALSE, frame = FALSE) 10 | # old_args = list(...) 11 | # new_args[names(old_args)] = old_args 12 | # 13 | # 14 | # ylim = old_args$ylim 15 | # ## Now check for log scales 16 | # if(is.null(ylim) && is_y(old_args$log)) { 17 | # ylim = range(y) *1.05 18 | # xlim = ylim 19 | # } 20 | # 21 | # if(is.null(ylim)) { 22 | # ticks_y = pretty(y) 23 | # ylim = range(ticks_y)*1.05 24 | # xlim = ylim 25 | # } else { 26 | # ticks_y = pretty(c(ylim, y)) 27 | # } 28 | # 29 | # new_args$ylim = ylim 30 | # new_args$xlim = xlim 31 | # 32 | # ## Need to remove main from new_args & pass it to do.call 33 | # new_args$main = NULL 34 | # res = do.call(stats_qqnorm_default, c(list(substitute(y), main=NULL), new_args)) 35 | # if(is_y(old_args$log)) { 36 | # ticks_y = axTicks(2) 37 | # } 38 | # 39 | # ## Check for plot.it 40 | # # Add axis 41 | # axis(2, tick = FALSE, las = 1) 42 | # abline(h = ticks_y, col = "grey90", lty = 2) 43 | # axis(1, tick = TRUE, lwd = 0, lwd.ticks = 1) 44 | # 45 | # ## Sneaky <- assignment 46 | # if(is.null(title_text <- old_args$main)) { 47 | # title_text = "Normal Q-Q Plot" 48 | # } 49 | # title(title_text, adj=1, 50 | # cex.main=1, font.main=2, col.main="black") 51 | # invisible(res) 52 | # 53 | # 54 | # } 55 | -------------------------------------------------------------------------------- /man/boxplot.prettyB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/boxplot.R 3 | \name{boxplot.prettyB} 4 | \alias{boxplot.prettyB} 5 | \alias{boxplot_p} 6 | \title{PrettyB boxplot.default function} 7 | \usage{ 8 | \method{boxplot}{prettyB}( 9 | x, 10 | ..., 11 | range = 1.5, 12 | width = NULL, 13 | varwidth = FALSE, 14 | notch = FALSE, 15 | outline = TRUE, 16 | names, 17 | plot = TRUE, 18 | border = par("fg"), 19 | col = NULL, 20 | log = "", 21 | pars = list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5), 22 | horizontal = FALSE, 23 | add = FALSE, 24 | at = NULL 25 | ) 26 | 27 | boxplot_p( 28 | x, 29 | ..., 30 | range = 1.5, 31 | width = NULL, 32 | varwidth = FALSE, 33 | notch = FALSE, 34 | outline = TRUE, 35 | names, 36 | plot = TRUE, 37 | border = par("fg"), 38 | col = NULL, 39 | log = "", 40 | pars = list(boxwex = 0.8, staplewex = 0.5, outwex = 0.5), 41 | horizontal = FALSE, 42 | add = FALSE, 43 | at = NULL 44 | ) 45 | } 46 | \arguments{ 47 | \item{x, ..., range, width}{See \code{?graphics::boxplot.default}} 48 | 49 | \item{varwidth, notch, outline}{See \code{?graphics::boxplot.default}} 50 | 51 | \item{names, plot, border}{See \code{?graphics::boxplot.default}} 52 | 53 | \item{col, log, pars}{See \code{?graphics::boxplot.default}} 54 | 55 | \item{horizontal, add, at}{See \code{?graphics::boxplot.default}} 56 | } 57 | \description{ 58 | This function overrides the default arguments. See 59 | \code{?graphics::boxplot.default} 60 | } 61 | -------------------------------------------------------------------------------- /R/ticks_title.R: -------------------------------------------------------------------------------- 1 | grid_lines_h = function(locations) { 2 | abline(h = locations, col = "grey90", lty = 1) 3 | } 4 | 5 | grid_lines_v = function(locations) { 6 | abline(v = locations, col = "grey90", lty = 1) 7 | } 8 | 9 | #' @rdname add_y_axis 10 | #' @export 11 | add_x_axis = function(ticks_x, labels = ticks_x, tick = TRUE, lwd = 0, 12 | lwd.ticks = 1) { 13 | axis(1, ticks_x, labels, 14 | tick = tick, lwd = lwd, lwd.ticks = lwd.ticks, 15 | col.axis = "grey30", col.ticks = "grey20") 16 | } 17 | 18 | #' @title Add tick marks to x/y axis 19 | #' 20 | #' A nicer set of default tick marks 21 | #' @param ticks_y location of y tick mark 22 | #' @param ticks_x location of x tick mark 23 | #' @param labels tick label 24 | #' @param tick Display tick mark 25 | #' @param lwd width of line 26 | #' @param lwd.ticks width of tick mark 27 | #' @export 28 | add_y_axis = function(ticks_y, labels = ticks_y, tick = TRUE, lwd = 0, 29 | lwd.ticks = 1) { 30 | axis(2, ticks_y, labels, 31 | tick = tick, las = 1, lwd.ticks = lwd.ticks, lwd = lwd, 32 | col.axis = "grey30", col.ticks = "grey20") 33 | } 34 | 35 | add_title = function(main = NULL) { 36 | if (!is.null(main)) { 37 | title(main, adj = 1, cex.main = 1.1, font.main = 2, 38 | col.main = par("col.main")) 39 | } 40 | } 41 | 42 | add_sub = function(sub = NULL) { 43 | if (!is.null(sub)) { 44 | title(sub = sub, 45 | adj = 1, cex.sub = 0.8, font.sub = 1, 46 | col.sub = par("col.main"), 47 | mgp = c(0.2, 0, 0)) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /man/plot.prettyB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/plot.R 3 | \name{plot.prettyB} 4 | \alias{plot.prettyB} 5 | \alias{plot_p} 6 | \title{PrettyB plot.default function} 7 | \usage{ 8 | \method{plot}{prettyB}( 9 | x, 10 | y = NULL, 11 | type = "p", 12 | xlim = NULL, 13 | ylim = NULL, 14 | log = "", 15 | main = NULL, 16 | sub = NULL, 17 | xlab = NULL, 18 | ylab = NULL, 19 | ann = par("ann"), 20 | axes = TRUE, 21 | frame.plot = axes, 22 | panel.first = NULL, 23 | panel.last = NULL, 24 | asp = NA, 25 | ... 26 | ) 27 | 28 | plot_p( 29 | x, 30 | y = NULL, 31 | type = "p", 32 | xlim = NULL, 33 | ylim = NULL, 34 | log = "", 35 | main = NULL, 36 | sub = NULL, 37 | xlab = NULL, 38 | ylab = NULL, 39 | ann = par("ann"), 40 | axes = TRUE, 41 | frame.plot = axes, 42 | panel.first = NULL, 43 | panel.last = NULL, 44 | asp = NA, 45 | ... 46 | ) 47 | } 48 | \arguments{ 49 | \item{x, y, type, log}{See \code{?graphics::plot.default}} 50 | 51 | \item{xlim, ylim}{See \code{?graphics::plot.default}} 52 | 53 | \item{main, sub, xlab, ylab}{See \code{?graphics::plot.default}} 54 | 55 | \item{ann, axes}{See \code{?graphics::plot.default}} 56 | 57 | \item{frame.plot, panel.first, panel.last}{See \code{?graphics::plot.default}} 58 | 59 | \item{asp}{See \code{?graphics::plot.default}} 60 | 61 | \item{...}{See \code{?graphics::plot.default}} 62 | } 63 | \description{ 64 | This function overrides the default arguments. See 65 | \code{?graphics::plot.default} 66 | } 67 | \examples{ 68 | plot_p(1:10, 1:10) 69 | } 70 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - main 5 | - master 6 | 7 | name: pkgdown 8 | 9 | jobs: 10 | pkgdown: 11 | runs-on: macOS-latest 12 | env: 13 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 14 | steps: 15 | - uses: actions/checkout@v2 16 | 17 | - uses: r-lib/actions/setup-r@v1 18 | 19 | - uses: r-lib/actions/setup-pandoc@v1 20 | 21 | - name: Query dependencies 22 | run: | 23 | install.packages('remotes') 24 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) 25 | writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") 26 | shell: Rscript {0} 27 | 28 | - name: Cache R packages 29 | uses: actions/cache@v2 30 | with: 31 | path: ${{ env.R_LIBS_USER }} 32 | key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} 33 | restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- 34 | 35 | - name: Install dependencies 36 | run: | 37 | remotes::install_deps(dependencies = TRUE) 38 | install.packages("pkgdown", type = "binary") 39 | shell: Rscript {0} 40 | 41 | - name: Install package 42 | run: R CMD INSTALL . 43 | 44 | - name: Deploy package 45 | run: | 46 | git config --local user.email "actions@github.com" 47 | git config --local user.name "GitHub Actions" 48 | Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)' 49 | -------------------------------------------------------------------------------- /man/hist.prettyB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/hist.R 3 | \name{hist.prettyB} 4 | \alias{hist.prettyB} 5 | \alias{hist_p} 6 | \title{PrettyB hist.default function} 7 | \usage{ 8 | \method{hist}{prettyB}( 9 | x, 10 | breaks = "Sturges", 11 | freq = NULL, 12 | probability = !freq, 13 | include.lowest = TRUE, 14 | right = TRUE, 15 | density = NULL, 16 | angle = 45, 17 | col = NULL, 18 | border = NULL, 19 | main = paste("Histogram of", xname), 20 | xlim = range(breaks), 21 | ylim = NULL, 22 | xlab = xname, 23 | ylab, 24 | axes = TRUE, 25 | plot = TRUE, 26 | labels = FALSE, 27 | nclass = NULL, 28 | warn.unused = TRUE, 29 | ... 30 | ) 31 | 32 | hist_p( 33 | x, 34 | breaks = "Sturges", 35 | freq = NULL, 36 | probability = !freq, 37 | include.lowest = TRUE, 38 | right = TRUE, 39 | density = NULL, 40 | angle = 45, 41 | col = NULL, 42 | border = NULL, 43 | main = paste("Histogram of", xname), 44 | xlim = range(breaks), 45 | ylim = NULL, 46 | xlab = xname, 47 | ylab, 48 | axes = TRUE, 49 | plot = TRUE, 50 | labels = FALSE, 51 | nclass = NULL, 52 | warn.unused = TRUE, 53 | ... 54 | ) 55 | } 56 | \arguments{ 57 | \item{x, breaks, freq, probability}{See \code{?graphics::hist.default}} 58 | 59 | \item{include.lowest, right, density}{See \code{?graphics::hist.default}} 60 | 61 | \item{angle, col, border, main}{See \code{?graphics::hist.default}} 62 | 63 | \item{xlim, ylim, xlab, ylab, axes, plot}{See \code{?graphics::hist.default}} 64 | 65 | \item{labels, nclass, warn.unused}{See \code{?graphics::hist.default}} 66 | 67 | \item{...}{See \code{?graphics::hist.default}} 68 | } 69 | \description{ 70 | This function overrides the default arguments. See 71 | \code{?graphics::hist.default} 72 | } 73 | \examples{ 74 | x = rlnorm(100) 75 | hist(x) 76 | } 77 | -------------------------------------------------------------------------------- /man/barplot.prettyB.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/barplot.R 3 | \name{barplot.prettyB} 4 | \alias{barplot.prettyB} 5 | \alias{barplot_p} 6 | \title{PrettyB barplot.default function} 7 | \usage{ 8 | \method{barplot}{prettyB}( 9 | height, 10 | width = 1, 11 | space = NULL, 12 | names.arg = NULL, 13 | legend.text = NULL, 14 | beside = FALSE, 15 | horiz = FALSE, 16 | density = NULL, 17 | angle = 45, 18 | col = NULL, 19 | border = par("fg"), 20 | main = NULL, 21 | sub = NULL, 22 | xlab = NULL, 23 | ylab = NULL, 24 | xlim = NULL, 25 | ylim = NULL, 26 | xpd = TRUE, 27 | log = "", 28 | axes = TRUE, 29 | axisnames = TRUE, 30 | cex.axis = par("cex.axis"), 31 | cex.names = par("cex.axis"), 32 | inside = TRUE, 33 | plot = TRUE, 34 | axis.lty = 0, 35 | offset = 0, 36 | add = FALSE, 37 | args.legend = NULL, 38 | ... 39 | ) 40 | 41 | barplot_p( 42 | height, 43 | width = 1, 44 | space = NULL, 45 | names.arg = NULL, 46 | legend.text = NULL, 47 | beside = FALSE, 48 | horiz = FALSE, 49 | density = NULL, 50 | angle = 45, 51 | col = NULL, 52 | border = par("fg"), 53 | main = NULL, 54 | sub = NULL, 55 | xlab = NULL, 56 | ylab = NULL, 57 | xlim = NULL, 58 | ylim = NULL, 59 | xpd = TRUE, 60 | log = "", 61 | axes = TRUE, 62 | axisnames = TRUE, 63 | cex.axis = par("cex.axis"), 64 | cex.names = par("cex.axis"), 65 | inside = TRUE, 66 | plot = TRUE, 67 | axis.lty = 0, 68 | offset = 0, 69 | add = FALSE, 70 | args.legend = NULL, 71 | ... 72 | ) 73 | } 74 | \arguments{ 75 | \item{height, width, space}{See \code{?graphics::barplot.default}} 76 | 77 | \item{names.arg, legend.text, beside}{See \code{?graphics::barplot.default}} 78 | 79 | \item{horiz, density, angle, col}{See \code{?graphics::barplot.default}} 80 | 81 | \item{border, main, sub, xlab, ylab}{See \code{?graphics::barplot.default}} 82 | 83 | \item{xlim, ylim, xpd, log, axes}{See \code{?graphics::barplot.default}} 84 | 85 | \item{axisnames, cex.axis, cex.names}{See \code{?graphics::barplot.default}} 86 | 87 | \item{inside, plot, axis.lty}{See \code{?graphics::barplot.default}} 88 | 89 | \item{offset, add, args.legend}{See \code{?graphics::barplot.default}} 90 | 91 | \item{...}{See \code{?graphics::barplot.default}} 92 | } 93 | \description{ 94 | This function overrides the default arguments. See 95 | \code{?graphics::barplot.default} 96 | } 97 | \examples{ 98 | barplot(VADeaths) 99 | } 100 | -------------------------------------------------------------------------------- /.github/workflows/check-standard.yaml: -------------------------------------------------------------------------------- 1 | # For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag. 2 | # https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions 3 | on: [push] 4 | name: R-CMD-check 5 | 6 | jobs: 7 | R-CMD-check: 8 | runs-on: ${{ matrix.config.os }} 9 | 10 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 11 | 12 | strategy: 13 | fail-fast: false 14 | matrix: 15 | config: 16 | - {os: windows-latest, r: 'release'} 17 | - {os: macOS-latest, r: 'release'} 18 | - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} 19 | - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} 20 | 21 | env: 22 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 23 | RSPM: ${{ matrix.config.rspm }} 24 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 25 | 26 | steps: 27 | - uses: actions/checkout@v2 28 | 29 | - uses: r-lib/actions/setup-r@v1 30 | with: 31 | r-version: ${{ matrix.config.r }} 32 | 33 | - uses: r-lib/actions/setup-pandoc@v1 34 | 35 | - name: Query dependencies 36 | run: | 37 | install.packages('remotes') 38 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) 39 | writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") 40 | shell: Rscript {0} 41 | 42 | - name: Cache R packages 43 | if: runner.os != 'Windows' 44 | uses: actions/cache@v2 45 | with: 46 | path: ${{ env.R_LIBS_USER }} 47 | key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} 48 | restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- 49 | 50 | - name: Install system dependencies 51 | if: runner.os == 'Linux' 52 | run: | 53 | while read -r cmd 54 | do 55 | eval sudo $cmd 56 | done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))') 57 | 58 | - name: Install dependencies 59 | run: | 60 | remotes::install_deps(dependencies = TRUE) 61 | remotes::install_cran("rcmdcheck") 62 | shell: Rscript {0} 63 | 64 | - name: Check 65 | env: 66 | _R_CHECK_CRAN_INCOMING_REMOTE_: false 67 | run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check") 68 | shell: Rscript {0} 69 | 70 | - name: Upload check results 71 | if: failure() 72 | uses: actions/upload-artifact@main 73 | with: 74 | name: ${{ runner.os }}-r${{ matrix.config.r }}-results 75 | path: check 76 | -------------------------------------------------------------------------------- /R/hist.R: -------------------------------------------------------------------------------- 1 | #' @title PrettyB hist.default function 2 | #' 3 | #' @description This function overrides the default arguments. See 4 | #' \code{?graphics::hist.default} 5 | #' @param x,breaks,freq,probability See \code{?graphics::hist.default} 6 | #' @param include.lowest,right,density See \code{?graphics::hist.default} 7 | #' @param angle,col,border,main See \code{?graphics::hist.default} 8 | #' @param xlim,ylim,xlab,ylab,axes,plot See \code{?graphics::hist.default} 9 | #' @param labels,nclass,warn.unused See \code{?graphics::hist.default} 10 | #' @param ... See \code{?graphics::hist.default} 11 | #' @export 12 | #' @examples 13 | #' x = rlnorm(100) 14 | #' hist(x) 15 | hist.prettyB = function(x, breaks = "Sturges", freq = NULL, probability = !freq, 16 | include.lowest = TRUE, right = TRUE, density = NULL, 17 | angle = 45, col = NULL, border = NULL, 18 | main = paste("Histogram of", xname), 19 | xlim = range(breaks), ylim = NULL, 20 | xlab = xname, ylab, axes = TRUE, 21 | plot = TRUE, labels = FALSE, nclass = NULL, 22 | warn.unused = TRUE, 23 | ...) { 24 | setup_prettyB() 25 | xname = paste(deparse(substitute(x), 500), collapse = "\n") 26 | # Unchanged Arguments 27 | args = list(...) 28 | args$x = x 29 | 30 | args$freq = freq 31 | if (!missing(probability)) args$probability = probability 32 | args$include.lowest = include.lowest 33 | args$right = right 34 | args$density = density 35 | args$angle = angle 36 | main = if (missing(main)) paste("Histogram of", xname) else main 37 | args$main = " " 38 | sub = if (!is.null(args$sub)) args$sub else NULL 39 | args$sub = NULL 40 | #args$xlim = xlim # Get xlim from hist_out below 41 | 42 | args$xlab = xlab 43 | if (!missing(ylab)) args$ylab = ylab 44 | args$axes = axes 45 | args$plot = plot 46 | args$labels = labels 47 | args$nclass = nclass 48 | args$warn.unused = warn.unused 49 | 50 | # Changed 51 | args$breaks = if (missing(breaks)) "Freedman-Diaconis" else breaks 52 | args$col = if (missing(col)) 1 else col 53 | args$border = if (missing(border)) "white" else border 54 | args$ylim = ylim 55 | if (isTRUE(args$add)) { 56 | return(do.call(graphics::hist.default, args)) 57 | } 58 | args$plot = FALSE 59 | hist_out = suppressWarnings(do.call(graphics::hist.default, args)) 60 | 61 | if (hist_out$equidist && (is.null(freq) || isTRUE(freq))) { 62 | args$freq = TRUE 63 | } else { 64 | args$freq = FALSE 65 | } 66 | 67 | if (is.null(args$ylab)) { 68 | args$ylab = if (isFALSE(args$freq)) "Density" else "Frequency" 69 | } 70 | args$plot = plot 71 | 72 | if (!is.null(args$ylim)) { 73 | ticks_y = pretty(args$ylim) 74 | } else if (is.null(args$freq) || isTRUE(args$freq)) { 75 | ticks_y = pretty(c(hist_out$counts, 0)) 76 | } else { 77 | ticks_y = pretty(c(hist_out$density, 0)) 78 | } 79 | 80 | if (is.null(args$ylim)) { 81 | args$ylim = range(ticks_y) 82 | } 83 | ticks_x = pretty(c(hist_out$breaks, args$xlim)) 84 | if (is.null(args$xlim)) { 85 | args$xlim = range(ticks_x) 86 | } 87 | args$axes = FALSE 88 | 89 | ## Don't display title - use title() 90 | graphics::plot.default(0, type = "n", 91 | xlim = args$xlim, ylim = args$ylim, 92 | bty = "n", axes = FALSE, 93 | ylab = args$ylab, xlab = args$xlab, 94 | panel.first = grid_lines_h(ticks_y)) 95 | args$add = TRUE 96 | hist_out = do.call(graphics::hist.default, args) 97 | 98 | abline(0, 0, col = "white") 99 | add_x_axis(ticks_x) 100 | add_y_axis(ticks_y, tick = FALSE) 101 | add_title(main) 102 | add_sub(sub) 103 | 104 | invisible(hist_out) 105 | } 106 | 107 | #' @rdname hist.prettyB 108 | #' @export 109 | hist_p = hist.prettyB 110 | -------------------------------------------------------------------------------- /vignettes/introduction.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Introduction to prettyB" 3 | author: "Colin Gillespie" 4 | vignette: > 5 | %\VignetteIndexEntry{Introduction to prettyB} 6 | \usepackage[utf8]{inputenc} 7 | %\VignetteEngine{knitr::rmarkdown} 8 | --- 9 | ```{r include = FALSE} 10 | knitr::opts_chunk$set(results = "hide", echo = TRUE ) 11 | set.seed(1) 12 | library(prettyB) 13 | ``` 14 | 15 | ## Introduction 16 | 17 | The __prettyB__ changes the default values of some standard plotting functions. 18 | It is __not__ a replacement for __ggplot2__ (or other plotting packages), 19 | instead, it's meant to just make the base graphics a little bit nicer. 20 | Due to the underlying structure of base graphics & S3 objects, some things aren't possible. 21 | In particular, changing how non-exported plot-methods are displayed. 22 | 23 | The general idea is the argument signitures are 24 | exactly the same as base graphics. But there are just a bit prettier. 25 | The current methods that are available are 26 | 27 | * `plot_p()` 28 | * `barplot_p()` 29 | * `boxplot_p()` 30 | * `hist_p()` 31 | 32 | ## Histogram examples 33 | 34 | The important point to note with this histogram method is the `breaks` 35 | argument now has a new default. Instead of `breaks = "Sturges"`, the 36 | default is `breaks = "fd"`. In general, this is a better default (but 37 | not always) 38 | ```{r, echo = c(-1, -2)} 39 | op = par(mfrow = c(1, 2)) 40 | reset_prettyB() 41 | x = rlnorm(100) 42 | hist(x) 43 | hist_p(x) 44 | ``` 45 | 46 | Standard arguments work as well 47 | ```{r, echo = c(-1, -2)} 48 | op = par(mfrow = c(1, 2)) 49 | reset_prettyB() 50 | y = rnorm(100) 51 | hist(y, main = "Base Graphics", sub = "Sub heading", 52 | xlab = "x-axis", ylab = "y-axis") 53 | hist_p(y, main = "prettyB", sub = "Sub heading", xlab = "x-axis", ylab = "y-axis") 54 | ``` 55 | 56 | Colours and frequencies also work as is expected 57 | ```{r, echo = c(-1, -2)} 58 | op = par(mfrow = c(1, 2)) 59 | reset_prettyB() 60 | z = rt(100, 4) 61 | hist(z, col = "grey60", freq = FALSE) 62 | hist_p(z, col = "grey60", freq = FALSE) 63 | ``` 64 | 65 | ## Barplot examples 66 | 67 | The `barplot()` has an amazing number of different configurations. 68 | 69 | ```{r, out.width="100%"} 70 | op = par(mfrow = c(1, 2)) 71 | reset_prettyB() 72 | library("grDevices") # for colours 73 | tN = table(stats::rpois(100, lambda = 3)) 74 | barplot(tN) 75 | barplot_p(tN) 76 | ``` 77 | 78 | The following examples are taken directly from the example page of `?barplot`. 79 | This means that sometimes the colours clash. 80 | 81 | ```{r} 82 | reset_prettyB() 83 | r = barplot(tN, col = rainbow(20)) 84 | #- type = "h" plotting *is* 'bar'plot 85 | lines(r, tN, type = "h", col = "red", lwd = 2) 86 | r = barplot_p(tN, col = rainbow(20)) 87 | #- type = "h" plotting *is* 'bar'plot 88 | lines(r, tN, type = "h", col = "red", lwd = 2) 89 | ``` 90 | 91 | Spacing and titles still work 92 | 93 | ```{r} 94 | reset_prettyB() 95 | barplot(tN, space = 1.5, axisnames = FALSE, 96 | sub = "barplot(..., space= 1.5, axisnames = FALSE)") 97 | barplot_p(tN, space = 1.5, axisnames = FALSE, 98 | sub = "barplot(..., space= 1.5, axisnames = FALSE)") 99 | ``` 100 | 101 | So does the `beside` argument. 102 | 103 | ```{r} 104 | reset_prettyB() 105 | barplot(VADeaths, beside = TRUE, 106 | col = c("lightblue", "mistyrose", "lightcyan", 107 | "lavender", "cornsilk"), 108 | legend = rownames(VADeaths), ylim = c(0, 100)) 109 | 110 | barplot_p(VADeaths, beside = TRUE, 111 | col = c("lightblue", "mistyrose", "lightcyan", 112 | "lavender", "cornsilk"), 113 | legend = rownames(VADeaths), ylim = c(0, 100)) 114 | ``` 115 | 116 | ## Other plotting methods 117 | 118 | ```{r} 119 | par(mfrow = c(1, 2)) 120 | plot_p(1:100, rnorm(100), xlab = "X", ylab = "Y") 121 | boxplot_p(rnorm(100), horizontal = TRUE, 122 | main = "A boxplot") 123 | ``` 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | editor_options: 4 | chunk_output_type: console 5 | --- 6 | 7 | 8 | 9 | ```{r, echo = FALSE} 10 | knitr::opts_chunk$set( 11 | collapse = TRUE, 12 | comment = "#>", 13 | fig.path = "man/figures/README-", 14 | cache = FALSE, 15 | fig.align = 'center' 16 | ) 17 | set.seed(1) 18 | ``` 19 | 20 | ## Prettified Base Graphics 21 | 22 | [![R-CMD-check](https://github.com/jumpingrivers/prettyB/workflows/R-CMD-check/badge.svg)](https://github.com/jumpingrivers/prettyB/actions) 23 | [![CRAN](http://www.r-pkg.org/badges/version/prettyB)](https://cran.r-project.org/package=prettyB) 24 | [![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html) 25 | [![Coverage status](https://codecov.io/gh/jumpingrivers/prettyB/branch/master/graph/badge.svg)](https://codecov.io/github/jumpingrivers/prettyB?branch=master) 26 | [![Downloads](http://cranlogs.r-pkg.org/badges/prettyB?color=brightgreen)](https://cran.r-project.org/package=prettyB) 27 | 28 | Anyone who uses R Base graphics, have a 100 and 1 tweaks that they use to make the 29 | figures more presentable. This package aims to capture the tweaks in one place. 30 | 31 | ## Installation 32 | 33 | The package is still being developed and the graphs are subject to change. 34 | The package is on CRAN and can be installed in the usual way 35 | ```{r eval = FALSE} 36 | install.packages("prettyB") 37 | ``` 38 | To install the dev version, try 39 | 40 | ```{r, eval=FALSE} 41 | devtools::install_github("jumpingrivers/prettyB") 42 | ``` 43 | The package can then be loaded in the usual way 44 | 45 | ```{r, message=FALSE} 46 | library("prettyB") 47 | ``` 48 | 49 | ## Usage 50 | 51 | All plotting functions work exactly as before, with the same inputs. The difference 52 | is that the defaults have been changed. For example, compare 53 | 54 | ```{r, plot-minimal,fig.width=12} 55 | op = par(mfrow = c(1, 2)) 56 | plot(iris$Sepal.Length, iris$Sepal.Width) 57 | plot_p(iris$Sepal.Length, iris$Sepal.Width) 58 | ``` 59 | 60 | When you first call a __prettyB__, it changes the underlying `par()` and 61 | `palette()`. You can reset this via 62 | 63 | ```{r} 64 | prettyB::reset_prettyB() 65 | ``` 66 | 67 | The core idea of __prettyB__ is that no new arguments are introducted to the 68 | plot functions. This means, that no changes to existing code 69 | are required 70 | 71 | ```{r, plot-minimal-full,fig.width=6, echo = -1} 72 | op = par(mfrow = c(1, 1)) 73 | plot_p(iris$Sepal.Length, iris$Sepal.Width, 74 | xlab = "Length", ylab = "Width", 75 | main = "The Iris data set", 76 | sub = "I hate this data too") 77 | ``` 78 | 79 | ## Other plots 80 | 81 | The package also prettifies other functions 82 | 83 | * Histograms 84 | ```{r, echo=c(-1, -2)} 85 | prettyB::reset_prettyB() 86 | par(mfrow = c(1, 2)) 87 | z = rt(100, 4) 88 | hist(z, main = "The t-distribution") 89 | hist_p(z, main = "The t-distribution") 90 | ``` 91 | * barplots 92 | ```{r, echo=-1} 93 | par(mfrow = c(1, 2), cex = 0.9) 94 | barplot(VADeaths, main = "Death Rates in Virginia") 95 | barplot_p(VADeaths, main = "Death Rates in Virginia") 96 | ``` 97 | 98 | ## Package Rationale 99 | 100 | This package is __not__ a replacement for __ggplot2__ or other R related plotting packages. Instead, it has a few simple aims 101 | 102 | * provide package authors a low dependency method of making their plots look pretty 103 | * provide academics with a way of generating nice plots, but not worry about future changes in R 104 | * provide a simple way for improving the look and feel of plots in teaching 105 | 106 | Since the generated plots by __prettyB__ use standard base graphics, with no new arguments, this makes plots future proof. As a fall-back, just remove the `_p`. 107 | 108 | I picked up the general style a few years ago, but the book 109 | [Fundamentals of Data Visualization](https://www.amazon.com/Fundamentals-Data-Visualization-Informative-Compelling/dp/1492031089/) has made it a bit 110 | more consist. The author also provided a free 111 | [online](https://serialmentor.com/dataviz/) version. 112 | 113 | ## Other information 114 | 115 | * If you have any suggestions or find bugs, please use the github [issue tracker](https://github.com/jumpingrivers/prettyB/issues) 116 | * Feel free to submit pull requests 117 | 118 | --- 119 | 120 | Development of this package was supported by [Jumping Rivers](https://www.jumpingrivers.com) 121 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ## Prettified Base Graphics 5 | 6 | [![R-CMD-check](https://github.com/jumpingrivers/prettyB/workflows/R-CMD-check/badge.svg)](https://github.com/jumpingrivers/prettyB/actions) 7 | [![CRAN](http://www.r-pkg.org/badges/version/prettyB)](https://cran.r-project.org/package=prettyB) 8 | [![Lifecycle: 9 | maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html) 10 | [![Coverage 11 | status](https://codecov.io/gh/jumpingrivers/prettyB/branch/master/graph/badge.svg)](https://codecov.io/github/jumpingrivers/prettyB?branch=master) 12 | [![Downloads](http://cranlogs.r-pkg.org/badges/prettyB?color=brightgreen)](https://cran.r-project.org/package=prettyB) 13 | 14 | Anyone who uses R Base graphics, have a 100 and 1 tweaks that they use 15 | to make the figures more presentable. This package aims to capture the 16 | tweaks in one place. 17 | 18 | ## Installation 19 | 20 | The package is still being developed and the graphs are subject to 21 | change. The package is on CRAN and can be installed in the usual way 22 | 23 | ``` r 24 | install.packages("prettyB") 25 | ``` 26 | 27 | To install the dev version, try 28 | 29 | ``` r 30 | devtools::install_github("jumpingrivers/prettyB") 31 | ``` 32 | 33 | The package can then be loaded in the usual way 34 | 35 | ``` r 36 | library("prettyB") 37 | ``` 38 | 39 | ## Usage 40 | 41 | All plotting functions work exactly as before, with the same inputs. The 42 | difference is that the defaults have been changed. For example, compare 43 | 44 | ``` r 45 | op = par(mfrow = c(1, 2)) 46 | plot(iris$Sepal.Length, iris$Sepal.Width) 47 | plot_p(iris$Sepal.Length, iris$Sepal.Width) 48 | #> 49 | ``` 50 | 51 | 52 | 53 | When you first call a **prettyB**, it changes the underlying `par()` and 54 | `palette()`. You can reset this via 55 | 56 | ``` r 57 | prettyB::reset_prettyB() 58 | ``` 59 | 60 | The core idea of **prettyB** is that no new arguments are introducted to 61 | the plot functions. This means, that no changes to existing code are 62 | required 63 | 64 | ``` r 65 | plot_p(iris$Sepal.Length, iris$Sepal.Width, 66 | xlab = "Length", ylab = "Width", 67 | main = "The Iris data set", 68 | sub = "I hate this data too") 69 | #> 70 | ``` 71 | 72 | 73 | 74 | ## Other plots 75 | 76 | The package also prettifies other functions 77 | 78 | - Histograms 79 | 80 | ``` r 81 | z = rt(100, 4) 82 | hist(z, main = "The t-distribution") 83 | hist_p(z, main = "The t-distribution") 84 | ``` 85 | 86 | 87 | 88 | - barplots 89 | 90 | ``` r 91 | barplot(VADeaths, main = "Death Rates in Virginia") 92 | barplot_p(VADeaths, main = "Death Rates in Virginia") 93 | ``` 94 | 95 | 96 | 97 | ## Package Rationale 98 | 99 | This package is **not** a replacement for **ggplot2** or other R related 100 | plotting packages. Instead, it has a few simple aims 101 | 102 | - provide package authors a low dependency method of making their 103 | plots look pretty 104 | - provide academics with a way of generating nice plots, but not worry 105 | about future changes in R 106 | - provide a simple way for improving the look and feel of plots in 107 | teaching 108 | 109 | Since the generated plots by **prettyB** use standard base graphics, 110 | with no new arguments, this makes plots future proof. As a fall-back, 111 | just remove the `_p`. 112 | 113 | I picked up the general style a few years ago, but the book 114 | [Fundamentals of Data 115 | Visualization](https://www.amazon.com/Fundamentals-Data-Visualization-Informative-Compelling/dp/1492031089/) 116 | has made it a bit more consist. The author also provided a free 117 | [online](https://serialmentor.com/dataviz/) version. 118 | 119 | ## Other information 120 | 121 | - If you have any suggestions or find bugs, please use the github 122 | [issue tracker](https://github.com/jumpingrivers/prettyB/issues) 123 | - Feel free to submit pull requests 124 | 125 | ------------------------------------------------------------------------ 126 | 127 | Development of this package was supported by [Jumping 128 | Rivers](https://www.jumpingrivers.com) 129 | -------------------------------------------------------------------------------- /R/plot.R: -------------------------------------------------------------------------------- 1 | is_x = function(chr) length(grep("x", chr)) > 0 2 | is_y = function(chr) length(grep("y", chr)) > 0 3 | # Extend axis for par(xaxs="i) 4 | extend_axis = function(lim, eps = 0.02) lim + c(-1, 1) * diff(lim) * eps 5 | 6 | 7 | #' @title PrettyB plot.default function 8 | #' 9 | #' @description This function overrides the default arguments. See 10 | #' \code{?graphics::plot.default} 11 | #' @param x,y,type,log See \code{?graphics::plot.default} 12 | #' @param xlim,ylim See \code{?graphics::plot.default} 13 | #' @param main,sub,xlab,ylab See \code{?graphics::plot.default} 14 | #' @param ann,axes See \code{?graphics::plot.default} 15 | #' @param frame.plot,panel.first,panel.last See \code{?graphics::plot.default} 16 | #' @param asp See \code{?graphics::plot.default} 17 | #' @param ... See \code{?graphics::plot.default} 18 | #' @importFrom graphics plot.default abline axTicks axis grid par title 19 | #' @importFrom grDevices xy.coords 20 | #' @rdname plot.prettyB 21 | #' @export 22 | #' @examples 23 | #' plot_p(1:10, 1:10) 24 | plot.prettyB = function(x, y = NULL, type = "p", xlim = NULL, ylim = NULL, 25 | log = "", main = NULL, sub = NULL, 26 | xlab = NULL, ylab = NULL, 27 | ann = par("ann"), axes = TRUE, 28 | frame.plot = axes, panel.first = NULL, 29 | panel.last = NULL, asp = NA, 30 | ...) { 31 | setup_prettyB() 32 | xlabel = if (!missing(x)) deparse(substitute(x)) 33 | ylabel = if (!missing(y)) deparse(substitute(y)) 34 | xy = xy.coords(x, y, xlabel, ylabel, log) 35 | xlab = if (is.null(xlab)) xy$xlab else xlab 36 | ylab = if (is.null(ylab)) xy$ylab else ylab 37 | 38 | #theme = current$theme 39 | ## Expand plot 40 | # op = set_par_minimal() 41 | # on.exit(par(op)) 42 | 43 | if (is.matrix(x)) { 44 | if (ncol(x) > 1 && !is.null(y)) { 45 | stop("x and y have different lengths (x is a matrix)", call. = FALSE) 46 | } 47 | if (ncol(x) > 1) { 48 | y = x[, 2] 49 | } 50 | x = x[, 1] 51 | } 52 | 53 | ## Do we have a y? 54 | if (is.null(y)) { 55 | x_tmp = seq_len(NROW(x)) # Handles the vector and matrix case 56 | y_tmp = x 57 | } else { 58 | x_tmp = x 59 | y_tmp = y 60 | } 61 | #xlim = c(1e1, 120) 62 | ## Now check for log scales 63 | #if (is.null(xlim) && is_x(log)) { 64 | # xlim = extend_axis(range(x_tmp)) 65 | #} 66 | 67 | if (is.null(xlim) && !is_x(log)) { 68 | ticks_x = pretty(x_tmp) 69 | xlim = extend_axis(range(ticks_x)) 70 | } else if (!is_x(log)) { 71 | ticks_x = pretty(xlim) 72 | xlim = range(ticks_x) 73 | } else if (is_x(log) && is.null(xlim)) { 74 | ticks_x = 10 ^ pretty(log10(x_tmp)) 75 | xlim = range(ticks_x) 76 | } #else { 77 | #ticks_x = 10^pretty(log10(xlim)) 78 | #xlim = range(ticks_x) 79 | #} 80 | ## Now check for log scales 81 | #if (is.null(ylim) && is_y(log)) { 82 | # ylim = extend_axis(range(y_tmp)) 83 | #} 84 | 85 | if (is.null(ylim) && !is_y(log)) { 86 | ticks_y = pretty(y_tmp) 87 | ylim = extend_axis(range(ticks_y)) 88 | } else if (!is_y(log)) { 89 | ticks_y = pretty(ylim) 90 | ylim = range(ticks_y) 91 | } else if (is_y(log) && is.null(ylim)) { 92 | ticks_y = 10 ^ pretty(log10(y_tmp)) 93 | ylim = range(ticks_y) 94 | } #else { 95 | # ticks_y = 10^pretty(log10(ylim)) 96 | # ylim = range(ticks_y) 97 | # } 98 | 99 | # Unchanged Arguments 100 | args = list(...) 101 | args$x = x 102 | args$y = y 103 | args$type = type 104 | args$log = log 105 | args$xlab = xlab 106 | args$ylab = ylab 107 | args$ann = ann 108 | args$frame.plot = FALSE 109 | args$panel.last = panel.last 110 | args$asp = asp 111 | 112 | # Changed Args 113 | args$xlim = xlim 114 | args$ylim = ylim 115 | args$main = NULL 116 | args$sub = NULL 117 | args$axes = FALSE 118 | 119 | ## Log scales are a pain; pretty doesn't work 120 | if (!(is_y(log) && !is.null(ylim))) { 121 | args$panel.first = substitute(grid_lines_h(ticks_y)) 122 | } 123 | 124 | if (is.null(args$pch)) args$pch = 21 125 | if (is.null(args$bg)) args$bg = 1 126 | 127 | # Call to default plot 128 | do.call(graphics::plot.default, args) 129 | message(args$xaxt) 130 | ## Now add in tick marks and labels 131 | 132 | if (is_x(log)) { 133 | ticks_x = axTicks(1) 134 | } 135 | if (is.null(args$xaxt) || args$xaxt != "n") { 136 | add_x_axis(ticks_x) 137 | } 138 | if (is_y(log)) { 139 | ticks_y = axTicks(2) 140 | } 141 | if (is.null(args$yaxt) || args$yaxt != "n") { 142 | 143 | add_y_axis(ticks_y, tick = FALSE) 144 | } 145 | 146 | if (is_y(log) && !is.null(ylim)) { 147 | grid_lines_h(ticks_y) 148 | } 149 | # Add axis & title 150 | add_title(main) 151 | add_sub(sub) 152 | invisible(NULL) 153 | } 154 | 155 | #' @rdname plot.prettyB 156 | #' @export 157 | plot_p = plot.prettyB 158 | -------------------------------------------------------------------------------- /tests/figs/boxplot/boxplot-horiz.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 0 45 | 1 46 | 2 47 | 3 48 | 4 49 | 50 | 51 | -------------------------------------------------------------------------------- /R/barplot.R: -------------------------------------------------------------------------------- 1 | #' @title PrettyB barplot.default function 2 | #' 3 | #' @description This function overrides the default arguments. See 4 | #' \code{?graphics::barplot.default} 5 | #' @param height,width,space See \code{?graphics::barplot.default} 6 | #' @param names.arg,legend.text,beside See \code{?graphics::barplot.default} 7 | #' @param horiz,density,angle,col See \code{?graphics::barplot.default} 8 | #' @param border,main,sub,xlab,ylab See \code{?graphics::barplot.default} 9 | #' @param xlim,ylim,xpd,log,axes See \code{?graphics::barplot.default} 10 | #' @param axisnames,cex.axis,cex.names See \code{?graphics::barplot.default} 11 | #' @param inside,plot,axis.lty See \code{?graphics::barplot.default} 12 | #' @param offset,add,args.legend See \code{?graphics::barplot.default} 13 | #' @param ... See \code{?graphics::barplot.default} 14 | #' @importFrom graphics barplot.default barplot 15 | #' @export 16 | #' @examples 17 | #' barplot(VADeaths) 18 | barplot.prettyB = function(height, width = 1, space = NULL, names.arg = NULL, 19 | legend.text = NULL, beside = FALSE, horiz = FALSE, 20 | density = NULL, angle = 45, col = NULL, 21 | border = par("fg"), main = NULL, 22 | sub = NULL, xlab = NULL, ylab = NULL, 23 | xlim = NULL, ylim = NULL, 24 | xpd = TRUE, log = "", axes = TRUE, 25 | axisnames = TRUE, cex.axis = par("cex.axis"), 26 | cex.names = par("cex.axis"), inside = TRUE, 27 | plot = TRUE, axis.lty = 0, offset = 0, 28 | add = FALSE, args.legend = NULL, ...) { 29 | setup_prettyB() 30 | 31 | # Unchanged Arguments 32 | args = list(...) 33 | args$height = height; args$width = width; args$space = space 34 | args$names.arg = names.arg; args$legend.text = legend.text 35 | args$beside = beside; args$horiz = horiz 36 | args$density = density; args$angle = angle 37 | # barplot normally takes care of this 38 | # But we do a pass through plot first for grid lines 39 | args$xlab = if (missing(xlab)) NA else xlab 40 | args$ylab = if (missing(ylab)) NA else ylab 41 | args$xpd = xpd; args$log = log 42 | args$axisnames = axisnames; args$cex.axis = cex.axis 43 | args$cex.names = cex.names 44 | # inside isn't actually implemented 45 | if (!missing(inside)) args$inside = inside 46 | args$plot = plot 47 | args$axis.lty = axis.lty 48 | args$offset = offset 49 | args$add = add 50 | args$args.legend = args.legend 51 | 52 | # Changes 53 | args$main = NULL # Move to top 54 | args$sub = NULL # Move to bottom 55 | if (!is.null(col)) { 56 | args$col = col 57 | } else if (is.matrix(height)) { 58 | args$col = seq_len(NROW(height)) 59 | } else { 60 | args$col = 1 61 | } 62 | args$border = if (missing(border)) NA else border 63 | args$axes = if (missing(axes)) FALSE else axes 64 | 65 | args$plot = FALSE 66 | res = do.call(graphics::barplot.default, args) 67 | if (isFALSE(plot)) return(res) 68 | args$plot = TRUE 69 | 70 | # Combinations of horizontal, beside, 71 | # specified xlim, ylim 72 | # Used on the "frequency/count" axis 73 | if (!is.null(xlim) && isTRUE(args$horiz)) { 74 | ticks = pretty(xlim) 75 | } else if (!is.null(ylim) && !isTRUE(args$horiz)) { 76 | ticks = pretty(ylim) 77 | } else if (is.matrix(height) && isFALSE(beside)) { 78 | ticks = pretty(c(0, colSums(height))) 79 | } else { 80 | ticks = pretty(c(0, height)) 81 | } 82 | 83 | # Find end of rectangles for unequal widths 84 | rect_up = res + width / 2 85 | rect_low = res - width / 2 86 | if (isTRUE(args$horiz)) { 87 | ticks_x = ticks 88 | args$xlim = range(ticks_x) 89 | args$ylim = if (missing(ylim)) range(0, rect_up + rect_low[1]) else ylim 90 | graphics::plot.default(0, type = "n", 91 | ylim = args$ylim, 92 | xlim = args$xlim, 93 | bty = "n", axes = FALSE, 94 | ylab = args$ylab, xlab = args$xlab, 95 | panel.first = grid_lines_v(ticks_x)) 96 | 97 | } else { 98 | ticks_y = ticks 99 | args$xlim = if (missing(xlim)) range(0, rect_up + rect_low[1]) else xlim 100 | args$ylim = range(ticks_y) 101 | graphics::plot.default(0, type = "n", 102 | xlim = args$xlim, 103 | ylim = args$ylim, 104 | bty = "n", axes = FALSE, 105 | ylab = args$ylab, xlab = args$xlab, 106 | panel.first = grid_lines_h(ticks_y)) 107 | } 108 | 109 | # Get axis labels and position 110 | if (is.matrix(res) && ncol(res) > 1) { 111 | lab_loc = colMeans(res) 112 | lab = colnames(height) 113 | } else { 114 | lab_loc = res 115 | if (is.matrix(height)) { 116 | lab = colnames(height) 117 | } else { 118 | lab_loc = res 119 | lab = names(height) 120 | } 121 | } 122 | 123 | if (isTRUE(args$horiz)) { 124 | add_x_axis(ticks_x, tick = FALSE) 125 | if (isTRUE(axisnames)) add_y_axis(lab_loc, labels = lab, tick = FALSE) 126 | } else { 127 | add_y_axis(ticks_y, tick = FALSE) 128 | if (isTRUE(axisnames)) add_x_axis(lab_loc, labels = lab, tick = FALSE) 129 | } 130 | add_title(main) 131 | add_sub(sub) 132 | 133 | args$add = TRUE 134 | args$axisnames = FALSE 135 | res = do.call(graphics::barplot.default, args) 136 | invisible(res) 137 | } 138 | 139 | 140 | #' @rdname barplot.prettyB 141 | #' @export 142 | barplot_p = barplot.prettyB 143 | -------------------------------------------------------------------------------- /R/boxplot.R: -------------------------------------------------------------------------------- 1 | boxplot.formula = function(formula, data = NULL, ..., subset, na.action = NULL, 2 | drop = FALSE, sep = ".", lex.order = FALSE) { 3 | if (missing(formula) || (length(formula) != 3L)) 4 | stop("'formula' missing or incorrect") 5 | m <- match.call(expand.dots = FALSE) 6 | if (is.matrix(eval(m$data, parent.frame()))) 7 | m$data <- as.data.frame(data) 8 | m$... <- m$drop <- m$sep <- m$lex.order <- NULL 9 | m$na.action <- na.action 10 | m[[1L]] <- quote(stats::model.frame) 11 | mf <- eval(m, parent.frame()) 12 | response <- attr(attr(mf, "terms"), "response") 13 | args = list(...) 14 | args$x = split(mf[[response]], mf[-response], drop = drop, 15 | sep = sep, lex.order = lex.order) 16 | do.call(boxplot.prettyB, args) 17 | } 18 | 19 | #' @title PrettyB boxplot.default function 20 | #' 21 | #' @description This function overrides the default arguments. See 22 | #' \code{?graphics::boxplot.default} 23 | #' @param x,...,range,width See \code{?graphics::boxplot.default} 24 | #' @param varwidth,notch,outline See \code{?graphics::boxplot.default} 25 | #' @param names,plot,border See \code{?graphics::boxplot.default} 26 | #' @param col,log,pars See \code{?graphics::boxplot.default} 27 | #' @param horizontal,add,at See \code{?graphics::boxplot.default} 28 | #' @importFrom graphics boxplot 29 | #' @export 30 | boxplot.prettyB = function(x, ..., range = 1.5, width = NULL, varwidth = FALSE, 31 | notch = FALSE, outline = TRUE, names, 32 | plot = TRUE, border = par("fg"), 33 | col = NULL, log = "", 34 | pars = list(boxwex = 0.8, staplewex = 0.5, 35 | outwex = 0.5), 36 | horizontal = FALSE, add = FALSE, at = NULL) { 37 | if (class(x) == "formula") { 38 | args = list(...) 39 | x = do.call(boxplot.formula, args) 40 | } 41 | setup_prettyB() 42 | ## Grab parameters 43 | args = list(...) 44 | args$x = x; args$range = range; args$width = width 45 | args$varwidth = varwidth; args$notch = notch; args$outline = outline 46 | if (!missing(names)) args$names = names 47 | args$plot = plot; args$border = border; args$log = log 48 | args$pars = pars; args$horizontal = horizontal; args$add = add 49 | args$at = at 50 | 51 | # Grab titles then at the end 52 | main = args$main; sub = args$sub 53 | args$main = args$sub = NULL 54 | 55 | # Parameters to be changed 56 | args$col = if (missing(col)) 1 else col 57 | args$border = if (missing(border)) "grey30" else border 58 | ## XXX: Needs split up. Could have a mixture here of pars & not pars args 59 | if (missing(pars)) { 60 | args$boxwex = if (is.null(args$boxwex)) 0.7 else args$boxwex 61 | args$staplewex = if (is.null(args$staplewex)) 0.35 else args$staplewex 62 | args$outwex = if (is.null(args$outwex)) 0.35 else args$outwex 63 | args$boxlwd = if (is.null(args$boxlwd)) 0.1 else args$boxlwd 64 | args$medlwd = if (is.null(args$medlwd)) 2 else args$medlwd 65 | args$whisklty = if (is.null(args$whisklty)) 1 else args$whisklty 66 | } else { 67 | args$boxwex = if (is.null(pars$boxwex)) 0.7 else pars$boxwex 68 | args$staplewex = if (is.null(pars$staplewex)) 0.35 else pars$staplewex 69 | args$outwex = if (is.null(pars$outwex)) 0.35 else pars$outwex 70 | args$boxlwd = if (is.null(pars$boxlwd)) 0.1 else pars$boxlwd 71 | args$medlwd = if (is.null(pars$medlwd)) 2 else pars$medlwd 72 | args$whisklty = if (is.null(pars$whisklty)) 1 else pars$whisklty 73 | } 74 | 75 | ## Plot first to get dimensions 76 | args$axes = args$plot = FALSE 77 | box_out = do.call(graphics::boxplot.default, args) 78 | if (isFALSE(plot)) return(box_out) 79 | args$plot = TRUE 80 | 81 | ## Create axis: need to consider 82 | ## log, horizontal, & at parameters 83 | ## XXX: Fix log axis 84 | at = if (!is.null(at)) at else NULL 85 | xlim = if (!is.null(args$xlim)) args$xlim else NULL 86 | ylim = if (!is.null(args$ylim)) args$ylim else NULL 87 | 88 | ## Axis labels 89 | if (isFALSE(args$horizontal)) { 90 | number_axis = get_number_axis(ylim, box_out, args$log) 91 | name_axis = get_name_axis(at, xlim, args$boxwex, box_out, args$log) 92 | } else { 93 | # Horizontal: xlim -> ylim & ylim -> xlim! 94 | number_axis = get_number_axis(ylim, box_out, args$log) 95 | name_axis = get_name_axis(at, xlim, args$boxwex, box_out, args$log) 96 | } 97 | args$xlim = name_axis$lim; args$ylim = number_axis$lim 98 | ticks_names = name_axis$ticks; ticks_numbers = number_axis$ticks 99 | 100 | ## Add grid lines 101 | if (isFALSE(add)) { 102 | args$yaxt = "n" 103 | box_out = do.call(graphics::boxplot.default, args) 104 | #if (is_y(log)) { 105 | # ticks_y = axTicks(2) 106 | #} 107 | if (isTRUE(args$horizontal)) { 108 | grid_lines_v(ticks_numbers) 109 | } else { 110 | grid_lines_h(ticks_numbers) 111 | } 112 | } 113 | 114 | args$yaxt = NULL; args$add = TRUE 115 | box_out = do.call(graphics::boxplot.default, args) 116 | ## Add axis & title 117 | if (isFALSE(add)) { 118 | if (isTRUE(args$horizontal)) { 119 | add_x_axis(ticks_numbers, tick = FALSE) 120 | add_y_axis(ticks_names, labels = box_out$names, tick = TRUE, lwd = 0, 121 | lwd.ticks = 1) 122 | } else { 123 | add_x_axis(ticks_names, labels = box_out$names, tick = TRUE, lwd = 0, 124 | lwd.ticks = 1) 125 | add_y_axis(ticks_numbers, tick = FALSE) 126 | } 127 | if (!is.null(main)) add_title(main) 128 | if (!is.null(sub)) add_sub(sub) 129 | } 130 | invisible(box_out) 131 | } 132 | 133 | #' @rdname boxplot.prettyB 134 | #' @export 135 | boxplot_p = boxplot.prettyB 136 | -------------------------------------------------------------------------------- /tests/figs/boxplot/boxplot-basic.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 1 51 | 2 52 | 3 53 | 4 54 | 55 | -------------------------------------------------------------------------------- /tests/figs/barplot/barplot-basic-no-axisnames.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 0 32 | 50 33 | 100 34 | 150 35 | 200 36 | 250 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /tests/figs/boxplot/boxplot-labels.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | X 35 | Y 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 0 62 | 1 63 | 2 64 | 3 65 | 4 66 | Main 67 | Sub 68 | 69 | -------------------------------------------------------------------------------- /tests/figs/barplot/barplot-basic.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 0 32 | 50 33 | 100 34 | 150 35 | 200 36 | 250 37 | Rural Male 38 | Rural Female 39 | Urban Male 40 | Urban Female 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /tests/figs/barplot/barplot-horiz.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 0 32 | 50 33 | 100 34 | 150 35 | 200 36 | 250 37 | Rural Male 38 | Rural Female 39 | Urban Male 40 | Urban Female 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x-xlab-ylab.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | X 42 | Y 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 2 51 | 4 52 | 6 53 | 8 54 | 10 55 | 0 56 | 2 57 | 4 58 | 6 59 | 8 60 | 10 61 | 62 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x-xlab.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | X 42 | 1:10 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 2 51 | 4 52 | 6 53 | 8 54 | 10 55 | 0 56 | 2 57 | 4 58 | 6 59 | 8 60 | 10 61 | 62 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x-ylab.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | Index 42 | Y 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 2 51 | 4 52 | 6 53 | 8 54 | 10 55 | 0 56 | 2 57 | 4 58 | 6 59 | 8 60 | 10 61 | 62 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | Index 42 | 1:10 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 2 51 | 4 52 | 6 53 | 8 54 | 10 55 | 0 56 | 2 57 | 4 58 | 6 59 | 8 60 | 10 61 | 62 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x-y-xlab-ylab.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | X 42 | Y 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 2 51 | 4 52 | 6 53 | 8 54 | 10 55 | 10 56 | 12 57 | 14 58 | 16 59 | 18 60 | 20 61 | 62 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x-y-xlab.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | X 42 | 11:20 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 2 51 | 4 52 | 6 53 | 8 54 | 10 55 | 10 56 | 12 57 | 14 58 | 16 59 | 18 60 | 20 61 | 62 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x-y-ylab.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 1:10 42 | Y 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 2 51 | 4 52 | 6 53 | 8 54 | 10 55 | 10 56 | 12 57 | 14 58 | 16 59 | 18 60 | 20 61 | 62 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x-y.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 1:10 42 | 11:20 43 | 44 | 45 | 46 | 47 | 48 | 49 | 0 50 | 2 51 | 4 52 | 6 53 | 8 54 | 10 55 | 10 56 | 12 57 | 14 58 | 16 59 | 18 60 | 20 61 | 62 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x-y-mat-single.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | Index 51 | x[, 1] 52 | 53 | 54 | 55 | 56 | 57 | 0 58 | 5 59 | 10 60 | 15 61 | 20 62 | 40 63 | 45 64 | 50 65 | 55 66 | 60 67 | 68 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x-y-mat.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | x[,1] 51 | x[,2] 52 | 53 | 54 | 55 | 56 | 57 | 40 58 | 45 59 | 50 60 | 55 61 | 60 62 | 10 63 | 15 64 | 20 65 | 25 66 | 30 67 | 68 | -------------------------------------------------------------------------------- /tests/figs/plotting/plot-x-y-mat-single-vector.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | x[, 1] 51 | 1:20 52 | 53 | 54 | 55 | 56 | 57 | 40 58 | 45 59 | 50 60 | 55 61 | 60 62 | 0 63 | 5 64 | 10 65 | 15 66 | 20 67 | 68 | -------------------------------------------------------------------------------- /tests/figs/barplot/barplot-labels.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | X 32 | Y 33 | 0 34 | 50 35 | 100 36 | 150 37 | 200 38 | 250 39 | Rural Male 40 | Rural Female 41 | Urban Male 42 | Urban Female 43 | Main 44 | Sub 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /tests/figs/histograms/hist-basic.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | x 33 | Frequency 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 0 76 | 2 77 | 4 78 | 6 79 | 8 80 | 10 81 | 12 82 | 0 83 | 5 84 | 10 85 | 15 86 | 20 87 | 25 88 | 30 89 | Histogram of x 90 | 91 | -------------------------------------------------------------------------------- /tests/figs/histograms/hist-density.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | x 33 | Density 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 0 76 | 2 77 | 4 78 | 6 79 | 8 80 | 10 81 | 12 82 | 0 83 | 0.1 84 | 0.2 85 | 0.3 86 | 0.4 87 | 0.5 88 | 0.6 89 | Histogram of x 90 | 91 | --------------------------------------------------------------------------------