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.9010
Version: 0.0.2.9011
Authors@R:
c(
person(
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(plot2,default)
S3method(plot2,density)
S3method(plot2,formula)
export(plot2)
importFrom(grDevices,adjustcolor)
importFrom(grDevices,hcl.colors)
importFrom(grDevices,hcl.pals)
importFrom(grDevices,palette)
Expand Down
6 changes: 3 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# plot2 0.0.2.9010 (development version)
# plot2 0.0.2.9011 (development version)

Breaking changes:

Expand All @@ -24,8 +24,8 @@ automatically adjusting plot characters and line types by groups. (#28
"top!", "left!", "topright!", etc. Users also gain finer control over many other
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)
- Add support for `"pointrange"`, `"errobar"`, and `"ribbon"` plot types. (#35
@vincentarelbundock, #40 and #46 @grantmcdermott)
- Support `grid = TRUE` as an alternative to `grid = grid()`. (#43
@grantmcdermott)

Expand Down
4 changes: 2 additions & 2 deletions R/by_aesthetics.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ by_col = function(ngrps = 1L, col = NULL, palette = NULL) {
by_pch = function(ngrps, type, pch=NULL) {

no_pch = FALSE
if (!type %in% c("p", "b", "o", "pointrange")) {
if (!type %in% c("p", "b", "o", "pointrange", "errorbar")) {
no_pch = TRUE
pch = NULL

Expand Down Expand Up @@ -116,7 +116,7 @@ by_pch = function(ngrps, type, pch=NULL) {
by_lty = function(ngrps, type, lty=NULL) {

# don't care about line type, return NULL
if (!type %in% c("l", "b", "o", "c", "h", "s", "S")) {
if (!type %in% c("l", "b", "o", "c", "h", "s", "S", "ribbon")) {
out = NULL

# special "by" convenience keyword
Expand Down
45 changes: 31 additions & 14 deletions R/plot2.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
#' lines, "o" for overplotted points and lines, "s" and "S" for stair steps
#' and "h" for histogram-like vertical lines. "n" does not produce
#' any points or lines.
#' - Additional plot2 types: "pointrange" draws point range plots and
#' "errorbar" draws error bar plots.
#' - Additional plot2 types: "pointrange", "errorbar", and "ribbon" for
#' drawing these respective plot types.
#' @param xlim the x limits (x1, x2) of the plot. Note that x1 > x2 is allowed
#' and leads to a ‘reversed axis’. The default value, NULL, indicates that
#' the range of the `finite` values to be plotted should be used.
Expand Down Expand Up @@ -117,7 +117,7 @@
#' @param ... other `graphical` parameters (see `par` and also the "Details"
#' section of `plot`).
#'
#' @importFrom grDevices palette palette.colors palette.pals hcl.colors hcl.pals
#' @importFrom grDevices adjustcolor palette palette.colors palette.pals hcl.colors hcl.pals
#' @importFrom graphics axis box grconvertX lines par plot.new plot.window points title
#'
#' @examples
Expand Down Expand Up @@ -274,8 +274,8 @@ plot2.default = function(
if (is.null(xlab)) xlab = deparse(substitute(x))
if (is.null(ylab)) ylab = deparse(substitute(y))

xlabs = NULL
if (type %in% c("pointrange", "errorbar")) {
xlabs = NULL
if (type %in% c("pointrange", "errorbar", "ribbon")) {
if (is.character(x)) x = as.factor(x)
if (is.factor(x)) {
## Need to maintain order that was observed in the original data
Expand Down Expand Up @@ -510,10 +510,10 @@ plot2.default = function(

# axes, plot.frame and grid
if (axes) {
if (type %in% c("pointrange", "errorbar") && !is.null(xlabs)) {
axis(1, at = xlabs, labels = names(xlabs))
if (type %in% c("pointrange", "errorbar", "ribbon") && !is.null(xlabs)) {
axis(1, at = xlabs, labels = names(xlabs))
} else {
axis(1)
axis(1)
}
axis(2)
}
Expand All @@ -526,16 +526,32 @@ plot2.default = function(
}
}

# polygons before lines
if (type == "ribbon") {
invisible(
lapply(
seq_along(split_data),
function(i) {
graphics::polygon(
x = c(split_data[[i]]$x, rev(split_data[[i]]$x)),
y = c(split_data[[i]]$ymin, rev(split_data[[i]]$ymax)),
col = adjustcolor(col[i], 0.2),
border = FALSE
)
}
)
)
}
## segments/arrows before points
if (type == "pointrange") {
invisible(
lapply(
seq_along(split_data),
function(i) {
graphics::segments(
x0 = seq_along(split_data[[i]]$x),
x0 = split_data[[i]]$x,
y0 = split_data[[i]]$ymin,
x1 = seq_along(split_data[[i]]$x),
x1 = split_data[[i]]$x,
y1 = split_data[[i]]$ymax,
col = col[i],
lty = lty[i]
Expand All @@ -544,15 +560,15 @@ plot2.default = function(
)
)
}
if (type == "errorbar") {
if (type == "errorbar") {
invisible(
lapply(
seq_along(split_data),
function(i) {
graphics::arrows(
x0 = seq_along(split_data[[i]]$x),
x0 = split_data[[i]]$x,
y0 = split_data[[i]]$ymin,
x1 = seq_along(split_data[[i]]$x),
x1 = split_data[[i]]$x,
y1 = split_data[[i]]$ymax,
col = col[i],
lty = lty[i],
Expand Down Expand Up @@ -583,7 +599,8 @@ plot2.default = function(
}
)
)
} else if (type %in% c("l", "o", "b", "c", "h", "s", "S")) {
} else if (type %in% c("l", "o", "b", "c", "h", "s", "S", "ribbon")) {
if (type=="ribbon") type = "l"
invisible(
lapply(
seq_along(split_data),
Expand Down
8 changes: 4 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ with(
)
```

### Point range and error bar plots
### Point ranges, error bars, and ribbon plots

`plot2` adds supports for point range and error bar plots via the `"pointrange"`
and `"errorbar"` type arguments. An obvious use-case is for regression
`plot2` adds supports for uncertainty intervals via the `"pointrange"`,
`"errorbar"`, `"ribbon"` type arguments. An obvious use-case is for regression
coefficient plots.

```{r pointrange, warning = FALSE}
Expand All @@ -249,7 +249,7 @@ with(
plot2(
x = term, y = estimate,
ymin = ci_low, ymax = ci_high,
type = "pointrange",
type = "pointrange", # or: "errobar", "ribbon"
pch = 19, col = "dodgerblue",
grid = TRUE,
main = "Effect on Temperature"
Expand Down
Loading