Skip to content

Commit

Permalink
Improve log plot logic
Browse files Browse the repository at this point in the history
  • Loading branch information
csgillespie committed Jul 22, 2019
1 parent eb7881b commit 5045d0e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 31 deletions.
34 changes: 23 additions & 11 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
Package: prettyB
Type: Package
Package: prettyB
Title: Pretty Base Graphics
Version: 0.2.1.9000
Authors@R: person(given="Colin", family="Gillespie",
email="[email protected]", role = c("aut", "cre"))
Version: 0.2.1.9001
Authors@R:
person(given = "Colin",
family = "Gillespie",
role = c("aut", "cre"),
email = "[email protected]")
Maintainer: Colin Gillespie <[email protected]>
Description: Drop-in replacements for standard base graphics functions. The
replacements are prettier versions of the originals.
URL: https://github.com/jumpingrivers/prettyB/
BugReports: https://github.com/jumpingrivers/prettyB//issues
Description: Drop-in replacements for standard base graphics
functions. The replacements are prettier versions of the originals.
License: GPL-2 | GPL-3
Imports: grDevices, stats, graphics
Suggests: testthat, vdiffr, covr, lintr, knitr
URL: https://github.com/jumpingrivers/prettyB/
BugReports: https://github.com/jumpingrivers/prettyB/issues
Imports:
graphics,
grDevices,
stats
Suggests:
covr,
knitr,
lintr,
testthat,
vdiffr
VignetteBuilder:
knitr
Encoding: UTF-8
LazyData: true
VignetteBuilder: knitr
RoxygenNote: 6.1.1
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ S3method(barplot,prettyB)
S3method(boxplot,prettyB)
S3method(hist,prettyB)
S3method(plot,prettyB)
export(add_x_axis)
export(add_y_axis)
export(barplot_p)
export(boxplot_p)
export(hist_p)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# prettyB (development version)
* Export add_y_axis & add_x_axis tick functions
* Improved `plot()` logic for the log scale

## Version 0.2.1
* Bug fix: Don't open plotting window when loading the package
Expand Down
64 changes: 44 additions & 20 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,43 @@ plot.prettyB = function(x, y = NULL, type = "p", xlim = NULL, ylim = NULL,
x_tmp = x
y_tmp = y
}

#xlim = c(1e1, 120)
## Now check for log scales
if (is.null(xlim) && is_x(log)) {
xlim = extend_axis(range(x_tmp))
}
#if (is.null(xlim) && is_x(log)) {
# xlim = extend_axis(range(x_tmp))
#}

if (is.null(xlim)) {
if (is.null(xlim) && !is_x(log)) {
ticks_x = pretty(x_tmp)
xlim = extend_axis(range(ticks_x))
} else {
ticks_x = pretty(c(xlim, x_tmp))
} else if (!is_x(log)) {
ticks_x = pretty(xlim)
xlim = range(ticks_x)
}

} else if (is_x(log) && is.null(xlim)) {
ticks_x = 10^pretty(log10(x_tmp))
xlim = range(ticks_x)
} #else {
#ticks_x = 10^pretty(log10(xlim))
#xlim = range(ticks_x)
#}
## Now check for log scales
if (is.null(ylim) && is_y(log)) {
ylim = extend_axis(range(y_tmp))
}
#if (is.null(ylim) && is_y(log)) {
# ylim = extend_axis(range(y_tmp))
#}

if (is.null(ylim)) {
if (is.null(ylim) && !is_y(log)) {
ticks_y = pretty(y_tmp)
ylim = extend_axis(range(ticks_y))
} else {
ticks_y = pretty(c(ylim, y_tmp))
} else if (!is_y(log)) {
ticks_y = pretty(ylim)
ylim = range(ticks_y)
}
} else if (is_y(log) && is.null(ylim)) {
ticks_y = 10^pretty(log10(y_tmp))
ylim = range(ticks_y)
} #else {
# ticks_y = 10^pretty(log10(ylim))
# ylim = range(ticks_y)
# }

# Unchanged Arguments
args = list(...)
Expand All @@ -94,25 +105,38 @@ plot.prettyB = function(x, y = NULL, type = "p", xlim = NULL, ylim = NULL,
args$main = NULL
args$sub = NULL
args$axes = FALSE
args$panel.first = substitute(grid_lines_h(ticks_y))

## Log scales are a pain; pretty doesn't work
if (!((is_y(log) && !is.null(ylim)))) {
args$panel.first = substitute(grid_lines_h(ticks_y))
}

if (is.null(args$pch)) args$pch = 21
if (is.null(args$bg)) args$bg = 1

# Call to default plot
do.call(graphics::plot.default, args)

message(args$xaxt)
## Now add in tick marks and labels

if (is_x(log)) {
ticks_x = axTicks(1)
}
if (is.null(args$xaxt) || args$xaxt != "n") {
add_x_axis(ticks_x)
}
if (is_y(log)) {
ticks_y = axTicks(2)
}
if (is.null(args$yaxt) || args$yaxt != "n") {

add_y_axis(ticks_y, tick = FALSE)
}

if (((is_y(log) && !is.null(ylim)))) {
grid_lines_h(ticks_y)
}
# Add axis & title
add_x_axis(ticks_x)
add_y_axis(ticks_y, tick = FALSE)
add_title(main)
add_sub(sub)
invisible(NULL)
Expand Down
12 changes: 12 additions & 0 deletions R/ticks_title.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ grid_lines_v = function(locations) {
abline(v = locations, col = "grey90", lty = 1)
}

#' @rdname add_y_axis
#' @export
add_x_axis = function(ticks_x, labels = ticks_x, tick = TRUE, lwd = 0,
lwd.ticks = 1) {
axis(1, ticks_x, labels,
tick = tick, lwd = lwd, lwd.ticks = lwd.ticks,
col.axis = "grey30", col.ticks = "grey20")
}

#' @title Add tick marks to x/y axis
#'
#' A nicer set of default tick marks
#' @param ticks_y location of y tick mark
#' @param ticks_x location of x tick mark
#' @param labels tick label
#' @param tick Display tick mark
#' @param lwd width of line
#' @param lwd.ticks width of tick mark
#' @export
add_y_axis = function(ticks_y, labels = ticks_y, tick = TRUE, lwd = 0,
lwd.ticks = 1) {
axis(2, ticks_y, labels,
Expand Down
33 changes: 33 additions & 0 deletions man/add_y_axis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5045d0e

Please sign in to comment.