Skip to content

Commit

Permalink
Merge pull request #4 from stefanedwards/devel
Browse files Browse the repository at this point in the history
Bump to 0.3.1
  • Loading branch information
stefanedwards committed Sep 12, 2017
2 parents b226b2d + 7136429 commit c27060f
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Type: Package
Title: Freshing Up your 'ggplot2' Plots
URL: https://github.com/stefanedwards/lemon
BugReports: https://github.com/stefanedwards/lemon/issues
Version: 0.3.0
Version: 0.3.1
Date: 2017-05-18
Authors@R: c(
person('Stefan McKinnon', 'Edwards', email='sme@iysik.com',
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

* **TODO:** Extend `lemon_print` to work on cross-tabulation tables.

# lemon 0.3.1

* `reposition_legend` now places legend *under* axis lines,
and further accepts x and y arguments for nudging.

# lemon 0.3.0

* Added `.dot` functions for supplying `setwd()` safe relative file paths.
Expand Down
30 changes: 25 additions & 5 deletions R/legends.r
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,21 @@ arrangeGrob <- gridExtra::arrangeGrob
#'
#' To modify the look of the legend, use themes and the natural ggplot functions
#' found in \code{\link[ggplot2]{guide_legend}}.
#'
#' \emph{Positioning} is done by argument \code{position} which places the panel
#' relative in \code{panel} (see below).
#' \code{position} resolves to three variables, \code{x}, \code{y}, and \code{just}.
#' \code{x} and \code{y} is the coordinate in \code{panel}, where the anchorpoint of
#' the legend (set via \code{just}) is placed.
#' In other words, \code{just='bottom right'} places the bottom right corner of
#' the legend at coordinates \code{(x,y)}.
#'
#' The positioning can be set by argument \code{position} alone, which can be
#' further nudged by setting \code{position}, \code{x}, and \code{y}.
#' Alternatively, manually positioning can be obtained by setting arguments.
#' \code{x}, \code{y}, and \code{just}.
#'
#' Panel name is by default \code{panel}, but when using facets it typically
#' \emph{Panel} name is by default \code{panel}, but when using facets it typically
#' takes the form \code{panel-{col}-{row}}, but not for wrapped facets.
#' Either print result from \code{\link[ggplot2]{ggplotGrob}} or use
#' \code{\link{gtable_show_names}} to display all the names of the gtable
Expand All @@ -176,7 +189,7 @@ arrangeGrob <- gridExtra::arrangeGrob
#'
#' @param aplot a ggplot2 or gtable object.
#' @param position Where to place the legend in the panel.
#' Overrules \code{x}, \code{y}, and \code{just} arguments.
#' Overrules \code{just} argument.
#' @param legend The legend to place, if \code{NULL} (default),
#' it is extracted from \code{aplot} if
#' this is a ggplot2 object.
Expand Down Expand Up @@ -238,8 +251,8 @@ reposition_legend <- function(aplot,
'right' = c(x=1, y=0.5),
'center' = c(x=0.5, y=0.5)
)
x = unit(just[1], 'npc')
y = unit(just[2], 'npc')
if (is.null(x)) x = unit(just[1], 'npc')
if (is.null(y)) y = unit(just[2], 'npc')
}
if (is.null(x) | is.null(y) | is.null(just)) {
stop('Please supply either `position`, or `x`, `y`, and `just` arguments.')
Expand Down Expand Up @@ -270,7 +283,14 @@ reposition_legend <- function(aplot,

legend$vp <- viewport(x=x, y=y, just=just, width=sum(legend$widths), height=sum(legend$heights))
legend$name <- name
.z <- z

# Place legend *under* the lowest axis-line, if z is Inf
if (is.infinite(z)) {
axes <- grepl('axis', aplot$layout$name)
.z <- min(aplot$layout$z[axes]) - 1
} else {
.z <- z
}
.clip <- clip

if (is.character(panel)) {
Expand Down
3 changes: 3 additions & 0 deletions R/lemon_print.r
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#' \preformatted{
#' knit_print.data.frame <- lemon_print
#' knit_print.table <- lemon_print
#' knit_print.grouped_df <- lemon_print # enableds dplyr results
#' knit_print.tibble <- lemon_print
#' knit_print.tbl <- lemon_print
#' }
#'
#' \strong{Note:} We are \emph{not} calling the function,
Expand Down
11 changes: 10 additions & 1 deletion man/lemon.Rd

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

3 changes: 3 additions & 0 deletions man/lemon_print.Rd

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

17 changes: 15 additions & 2 deletions man/reposition_legend.Rd

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

2 changes: 1 addition & 1 deletion vignettes/capped-axes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ You've seen them above.
The following table summarises the connection between ggplot2's coord functions
and those of lemon.

ggplot2 | splot's flexible | splot's short hand
ggplot2 | lemon's flexible | lemon's short hand
------------------|--------------------|--------------------
`coord_cartesian` | `coord_flex_cart` | `coord_capped_cart`
`coord_flip` | `coord_flex_flip` | `coord_capped_flip`
Expand Down
25 changes: 24 additions & 1 deletion vignettes/legends.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ knitr::opts_template$set(smallfigure = list(fig.height=2, fig.width=3))
## Legend functions

* Extract legend: `g_legend`
* Combine multiple plots, but use only _one_ legend: `grid_arrange_shared_legend`
* Reposition legend onto the actual plot: `reposition_legend`
* Combine multiple plots, but use only _one_ legend: `grid_arrange_shared_legend`

## Reposition legend onto plotting panel

Expand Down Expand Up @@ -97,6 +97,29 @@ d2 <- d + aes(shape=cut) +
reposition_legend(d2, 'left')
```

### Legends are placed *under* axis lines

The guidebox uses a solid background (subject to the chosen theme),
and prior to lemon version 0.3.1, the entire legend was placed as the *top most*
element.
In the examples above, this was not an issue.
With axis lines drawn, this effectively overpainted some of the axis
(same applies to the panel border).

The guidebox is therefore placed *under* the lowest axis line,
*if and only if* `z = Inf`.
To place as top most, specify a large z-index.

```{r fig.cap='Legend is drawn *under* axis lines.'}
reposition_legend(d + theme_classic(), 'top left')
```

To adjust the guidebox so it does not overpaint the panel border, use arguments
`x` and `y`:

```{r fig.cap='Legend has to be nudged to not overpaint panel border.'}
reposition_legend(d + theme_bw(), 'top left', x=0.002, y=1-0.002)
```

### Warning regarding extracting legend

Expand Down
2 changes: 1 addition & 1 deletion vignettes/lemon_print.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ vignette: >
%\VignetteEncoding{UTF-8}
---

This vignette demonstrates how load the `splot` package automatically
This vignette demonstrates how load the `lemon` package automatically
enables pretty printing of data frames with knitr's `kable`.

The beauty of defining the function `knit_print.data.frame` is that when
Expand Down

0 comments on commit c27060f

Please sign in to comment.