Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: plot2
Type: Package
Title: Lightweight extension of base R plot
Version: 0.0.2.9008
Version: 0.0.2.9009
Authors@R:
c(
person(
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# plot2 0.0.2.9008 (development version)
# plot2 0.0.2.9009 (development version)

Breaking changes:

Expand Down Expand Up @@ -26,6 +26,8 @@ aspects of the legend via the new unified `legend` argument, including changing
labels, turning of the legend title, and so on. (#34 @grantmcdermott)
- Add support for `type="pointrange"` and `type="errobar"` plots (#35
@vincentarelbundock and #40 @grantmcdermott)
- Support `grid = TRUE` as an alternative to `grid = grid()`. (#43
@grantmcdermott)

Bug fixes:

Expand Down
23 changes: 16 additions & 7 deletions R/plot2.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@
#' the plot. Use `graphical parameter` "xaxt" or "yaxt" to suppress just one of
#' the axes.
#' @param frame.plot a logical indicating whether a box should be drawn around
#' the plot.
#' @param grid a panel grid plotting function like `grid()`. This argument
#' replaces the `panel.first` and `panel.last` arguments from base `plot()`
#' and tries to make the process more seemless with better default behaviour.
#' the plot. Can also use `frame` as an acceptable argument alias.
#' @param grid argument for plotting a background panel grid, one of either:
#' - a logical (i.e., `TRUE` to draw the grid), or
#' - a panel grid plotting function like `grid()`.
#' Note that this argument replaces the `panel.first` and `panel.last`
#' arguments from base `plot()` and tries to make the process more seemless
#' with better default behaviour. Default is not to draw a grid.
#' @param asp the y/xy/x aspect ratio, see `plot.window`.
#' @param palette one of the following options:
#' - NULL (default), in which case the palette will be determined by the
Expand Down Expand Up @@ -201,7 +204,7 @@
#' type = "b", pch = 16,
#' palette = palette.colors(palette = "Tableau 10", alpha = 0.5),
#' main = "Daily temperatures by month",
#' frame.plot = FALSE, grid = grid()
#' frame = FALSE, grid = TRUE
#' )
#'
#' # For nice out-of-the-box themes, we recommend the `basetheme` package.
Expand Down Expand Up @@ -513,8 +516,14 @@ plot2.default = function(
axis(2)
}
if (frame.plot) box()
if (!is.null(grid)) grid

if (!is.null(grid)) {
if (is.logical(grid)) {
if (isTRUE(grid)) grid()
} else {
grid
}
}

## segments/arrows before points
if (type == "pointrange") {
invisible(
Expand Down
60 changes: 35 additions & 25 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ using modern colour palettes. Coincident with this grouping support, `plot2()`
also produces automatic legends with scope for further customization. While the
package offers several other enhancements, it tries as far as possible to be a
drop-in replacement for the equivalent base plot function. Users should
(generally) be able to swap a valid `plot()` call with `plot2()` without any
generally be able to swap a valid `plot()` call with `plot2()` without any
changes to the expected output.

## Examples
Expand Down Expand Up @@ -124,7 +124,6 @@ old StackOverflow thread for a longer discussion.]
```{r by}
# plot2(airquality$Day, airquality$Temp, by = airquality$Month) # same as below
with(airquality, plot2(Day, Temp, by = Month))

```

An even more convenient approach is to use the equivalent formula syntax. Just
Expand Down Expand Up @@ -217,12 +216,15 @@ plot2(
Note that legend position keywords without the exclamation point (i.e., for
inside the plot area) should still as per normal. Grouped density plot example:

```{r desnity_topright}
with(airquality, plot2(
density(Temp),
by = Month,
legend = legend("topright", bty = "o")
))
```{r density_topright}
with(
airquality,
plot2(
density(Temp),
by = Month,
legend = legend("topright", bty = "o")
)
)
```

### Point range and error bar plots
Expand All @@ -232,17 +234,24 @@ and `"errorbar"` type arguments. An obvious use-case is for regression
coefficient plots.

```{r pointrange, warning = FALSE}
mod = lm(Temp ~ 0 + factor(Month), airquality)
coefs = data.frame(names(coef(mod)), coef(mod), confint(mod))
coefs = setNames(coefs, c("term", "estimate", "ci_low", "ci_high"))
aq = airquality
aq$mnth = factor(month.abb[aq$Month], levels = month.abb)
mod = lm(Temp ~ 0 + mnth, aq)
coeftab = data.frame(
gsub("mnth", "", names(coef(mod))),
coef(mod),
confint(mod)
) |>
setNames(c("term", "estimate", "ci_low", "ci_high"))

with(
coefs,
coeftab,
plot2(
x = term, y = estimate,
ymin = ci_low, ymax = ci_high,
type = "pointrange",
pch = 19,
pch = 19, col = "dodgerblue",
grid = TRUE,
main = "Effect on Temperature"
)
)
Expand All @@ -251,14 +260,15 @@ with(
### Customization

Customizing your plots further is straightforward, whether that is done by
changing global parameters or invoking `plot2` arguments. Here's a quick
penultimate example, where we change our point character and font family
globally, add some transparency to our colour palette, and use Tufte-style
floating axes with a background panel grid.
changing global parameters (via `par`) or invoking `plot2` arguments. Here's a
quick penultimate example, where we change our point character, tick labels, and
font family globally, before adding some transparency to our colour palette, and
use Tufte-style floating axes with a background panel grid.

```{r hershey_plus}
par(
pch = 16, # Filled points as default
pch = 16, # Filled points as default
las = 1, # Horizontal axis tick labels
family = "HersheySans" # Use a (built-in) Hershey font instead of Arial default
)

Expand All @@ -268,18 +278,18 @@ plot2(
type = "b",
palette = palette.colors(palette = "Tableau 10", alpha = 0.5),
main = "Daily temperatures by month",
frame.plot = FALSE, grid = grid()
frame = FALSE, grid = TRUE
)
```

The use of `par` in the above example again underscores the correspondence with
the base graphics system. Because `plot2` is effectively a convenience wrapper
around base `plot`, any global elements that you have set for the latter should
carry over to the former. For nice out-of-the-box themes, we recommend the
**basetheme** package.
At the risk of repeating ourselves, the use of `par` in the previous example
again underscores the correspondence with the base graphics system. Because
`plot2` is effectively a convenience wrapper around base `plot`, any global
elements that you have set for the latter should carry over to the former. For
nice out-of-the-box themes, we recommend the **basetheme** package.

```{r basethme_royal}
par(family = "", pch = 15) # revert/change global changes from above
par(pch = 15, las = 0, family = "") # change/revert global changes from above

library(basetheme)
basetheme("royal") # or "clean", "dark", "ink", "brutal", etc.
Expand Down
Loading