Skip to content

Commit

Permalink
Vignette update
Browse files Browse the repository at this point in the history
  • Loading branch information
aphalo committed Nov 7, 2023
1 parent c8e1e77 commit cdddfe1
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 125 deletions.
23 changes: 2 additions & 21 deletions R/position-nudge-stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,6 @@
#' scale_fill_grey(start = 0.7, end = 0.3) +
#' facet_wrap(facets = vars(Container))
#'
#' ggplot(birch_dw.df,
#' aes(y = dry.weight * 1e-3, x = Density, fill = Part)) +
#' stat_summary(geom = "col", fun = mean,
#' position = "stack", alpha = 0.7, width = 0.67) +
#' # error bars for each stack bar
#' stat_summary(geom = "linerange", fun.data = mean_cl_normal,
#' position = position_stack_minmax(x = -0.1)) +
#' # error bar for the total
#' stat_summary(data = birch.df, aes(y = (dwstem + dwroot) * 1e-3, fill = NULL),
#' geom = "linerange", linewidth = 0.75,
#' position = position_nudge(x = 0.1), fun.data = mean_cl_normal) +
#' labs(y = "Seedling dry mass (g)") +
#' scale_fill_grey(start = 0.7, end = 0.3) +
#' facet_wrap(facets = vars(Container))
#'

position_stacknudge <-
function(vjust = 1,
Expand All @@ -166,16 +151,12 @@ position_stacknudge <-
none = function(x) {1},
split = sign,
split.y = function(x) {1},
split.x = sign,
center = sign,
function(x) {1}),
split.x = sign),
.fun_y = switch(direction,
none = function(x) {1},
split = sign,
split.x = function(x) {1},
split.y = sign,
center = sign,
function(x) {1}),
split.y = sign),
kept.origin = kept.origin,
vjust = vjust,
reverse = reverse
Expand Down
15 changes: 0 additions & 15 deletions man/position_stacknudge.Rd

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

16 changes: 16 additions & 0 deletions vignettes/grammar-extensions.R
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,22 @@ ggplot(data = df, aes(x1, x2, group = grp)) +
theme(legend.position = "none")


## -----------------------------------------------------------------------------
ggplot(birch_dw.df,
aes(y = dry.weight * 1e-3, x = Density, fill = Part)) +
stat_summary(geom = "col", fun = mean,
position = "stack", alpha = 0.7, width = 0.67) +
# error bars for each stack bar
stat_summary(geom = "linerange", fun.data = mean_cl_normal,
position = position_stack_minmax(x = -0.1)) +
# error bar for the total
stat_summary(data = birch.df, aes(y = (dwstem + dwroot) * 1e-3, fill = NULL),
geom = "linerange", linewidth = 0.75,
position = position_nudge(x = 0.1), fun.data = mean_cl_normal) +
labs(y = "Seedling dry mass (g)") +
scale_fill_grey(start = 0.7, end = 0.3) +
facet_wrap(facets = vars(Container))

## -----------------------------------------------------------------------------
jitter <- position_jitter(width = 0.2, height = 2, seed = 123)

Expand Down
40 changes: 35 additions & 5 deletions vignettes/grammar-extensions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ lines, or with respect to a polynomial or smoothing spline fitted
on-the-fly to the the observations.

A limitation of 'ggplot2' is that only one position function can be used
in a layer. This makes it difficult to add text labels to stacked or
in a layer. This makes it difficult to add data labels to stacked or
dodged bars or columns, or the labelling of points that have been
jittered. The solution provided by 'ggpp' are wrappers on these position
functions from 'ggplot2'. These wrappers add support for nudging.
Expand Down Expand Up @@ -333,10 +333,10 @@ There is a storage-use cost in saving the original position, by making the
consideration when `data` has many rows. In such cases, when a counterpart
position function that does not keep the original coordinates exists, it is wise
not to use the new "keep" position functions in combination with geometries that
cannot make use of the additional information stored by them. When using those
new position functions with no counterpart in 'ggplot2' the keeping of the
original position can be disabled by passing `kept.position = "none"` when they
are called.
cannot make use of the additional information stored by them. Alternatively, and
when using those new position functions with no counterpart in 'ggplot2' the
keeping of the original position can be disabled by passing `kept.position =
"none"` as an additional argument when they are called.

## Local density of observations

Expand Down Expand Up @@ -1894,6 +1894,36 @@ Here we nudge the labels down from the top of each stacked bar with `vjust = 1`.
```

### position_stack_minmax()

Functions `position_stack()` and `position_stack_nudge()` displace the position
of a variable mapped to only one of `y`, `ymax` or `ymin`. So they cannot
be used with geometries `geom_linerange()`, `geom_pointrange()` or
`geom_errorbar()`. `position_stack_minmax()` can be used with these geometries
as it displaces `y`, `ymin` and `ymax` together by the same distance. Like
`position_stack_nudge()` it also supports nudging. Thus, this new position
functions makes it possible to add error bars to each member of stacked
colums, the corresponding error bar. As nudging is also supported, it is
possible as shown below, to also an additional error bar for the total quantity
represented by the stacked bars.

```{r}
ggplot(birch_dw.df,
aes(y = dry.weight * 1e-3, x = Density, fill = Part)) +
stat_summary(geom = "col", fun = mean,
position = "stack", alpha = 0.7, width = 0.67) +
# error bars for each stack bar
stat_summary(geom = "linerange", fun.data = mean_cl_normal,
position = position_stack_minmax(x = -0.1)) +
# error bar for the total
stat_summary(data = birch.df, aes(y = (dwstem + dwroot) * 1e-3, fill = NULL),
geom = "linerange", linewidth = 0.75,
position = position_nudge(x = 0.1), fun.data = mean_cl_normal) +
labs(y = "Seedling dry mass (g)") +
scale_fill_grey(start = 0.7, end = 0.3) +
facet_wrap(facets = vars(Container))
```

### position_jitternudge()

When combining jitter and nudge, we can nudge away from the jittered positions
Expand Down
Loading

0 comments on commit cdddfe1

Please sign in to comment.