Skip to content

Commit

Permalink
v0.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
yjunechoe committed Apr 17, 2022
1 parent 16f7220 commit ba96242
Show file tree
Hide file tree
Showing 51 changed files with 282 additions and 69 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ggtrace
Type: Package
Title: Programmatically explore, debug, and manipulate ggplot internals
Version: 0.5.0.9000
Version: 0.5.1
Authors@R: c(
person(given = "June",
family = "Choe",
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# ggtrace (development version)

- `cond` argument of workflow functions now support integer shorthand for conditioning on the counter. E.g., `cond = 1L` is converted to `cond = quote(._counter_ == 1L)`
## ggtrace 0.5.1

- `cond` argument of workflow functions now support integer shorthand for conditioning on the counter. E.g., `cond = 1L` is converted to `cond = quote(._counter_ == 1L)`. Multi-length integer vector is supported for highjack workflows.

## ggtrace 0.5.0

Expand Down
4 changes: 2 additions & 2 deletions R/workflows-highjack.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#'
ggtrace_highjack_args <- function(x, method, cond = 1L, values, draw = TRUE) {

cond <- resolve_cond(cond)
cond <- resolve_cond(cond, multiple = TRUE)

wrapper_env <- rlang::current_env()
._counter_ <- 0L
Expand Down Expand Up @@ -177,7 +177,7 @@ ggtrace_highjack_args <- function(x, method, cond = 1L, values, draw = TRUE) {
#'
ggtrace_highjack_return <- function(x, method, cond = 1L, value = quote(returnValue()), draw = TRUE) {

cond <- resolve_cond(cond)
cond <- resolve_cond(cond, multiple = TRUE)

wrapper_env <- rlang::current_env()
._counter_ <- 0L
Expand Down
10 changes: 5 additions & 5 deletions R/workflows-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ print.ggtrace_highjacked <- function(x, ...) {
ggdraw_silent(x)
}

resolve_cond <- function(x) {
resolve_cond <- function(x, multiple = FALSE) {
if (is.numeric(x)) {
if (length(x) > 1L) {
rlang::warn("`cond` is length > 1 and only the first element will be used")
x <- x[1]
if (multiple && length(x) > 1L) {
x <- rlang::expr(._counter_ %in% !!as.integer(x))
} else {
x <- rlang::expr(._counter_ == !!as.integer(x))
}
x <- rlang::expr(._counter_ == !!as.integer(x))
}
x
}
67 changes: 65 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ knitr::opts_chunk$set(
#### **Programmatically explore, debug, and manipulate ggplot internals**

<!-- badges: start -->
[![](https://img.shields.io/badge/devel%20version-0.5.0.9000-gogreen.svg)](https://github.com/yjunechoe/ggtrace)
[![](https://img.shields.io/badge/devel%20version-0.5.1-gogreen.svg)](https://github.com/yjunechoe/ggtrace)
<!-- badges: end -->

### **Installation**
Expand All @@ -41,7 +41,7 @@ remotes::install_github("yjunechoe/ggtrace")
```

```{r setup}
library(ggtrace) # v0.5.0.9000
library(ggtrace) # v0.5.1
```

More on the 📦 package website: [https://yjunechoe.github.io/ggtrace](https://yjunechoe.github.io/ggtrace)
Expand Down Expand Up @@ -515,3 +515,66 @@ with_ggtrace(
)
```

## **Case study: Data-driven legends**

Suppose we want to change the fill legend key from squares to the actual violins drawn for each category:

```{r}
violin_plot <- dplyr::starwars %>%
filter(species == "Human", !is.na(height)) %>%
ggplot(aes(gender, height)) +
geom_violin(aes(fill = gender)) +
theme(
legend.key.size = unit(1, "in"),
legend.key = element_rect(color = "grey", fill = NA)
)
violin_plot
```

We know that each violin is drawn by `GeomViolin$draw_group`, called for each group in the plot.

```{r}
ggtrace_inspect_n(violin_plot, GeomViolin$draw_group)
```

Let's grab the plotted violins with `ggtrace_inspect_return()`

```{r}
violins <- lapply(1:2, ggtrace_inspect_return, x = violin_plot, method = GeomViolin$draw_group)
violins
```

Then, we can use `ggtrace_highjack_return()` to highjack the return value of the key drawing method `GeomViolin$draw_key` such that it returns the output from the `draw_group` method instead. We set `cond = 1:2` and value to `substitute(violins[[._counter_]])` so that the `draw_key` method returns the first violin as the first legend key, and the second violin as the second legend key.

```{r}
ggtrace_highjack_return(
violin_plot,
GeomViolin$draw_key,
cond = 1:2,
value = substitute(violins[[._counter_]])
)
```

Note that violins are drawn with respect to the panel coordinate system, because that's how the `draw_group` method does things. We create and use the function `center_grob()` to center them before they're passed to the `value` argument of `ggtrace_highjack_return`.

```{r}
library(grid)
center_grob <- function(grob) {
x_range <- range(unclass(grob$x))
y_range <- range(unclass(grob$y))
editGrob(grob, vp = viewport(
xscale = x_range, yscale = y_range,
width = diff(x_range), height = diff(y_range)
))
}
violins_stretched <- lapply(violins, center_grob)
ggtrace_highjack_return(
violin_plot,
GeomViolin$draw_key,
cond = 1:2,
value = substitute(violins_stretched[[._counter_]])
)
```
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<!-- badges: start -->

[![](https://img.shields.io/badge/devel%20version-0.5.0.9000-gogreen.svg)](https://github.com/yjunechoe/ggtrace)
[![](https://img.shields.io/badge/devel%20version-0.5.1-gogreen.svg)](https://github.com/yjunechoe/ggtrace)
<!-- badges: end -->

### **Installation**
Expand All @@ -17,7 +17,7 @@ You can install the development version from
# install.packages("remotes")
remotes::install_github("yjunechoe/ggtrace")

library(ggtrace) # v0.5.0.9000
library(ggtrace) # v0.5.1

More on the 📦 package website: <https://yjunechoe.github.io/ggtrace>

Expand Down Expand Up @@ -632,3 +632,78 @@ as well!
)

<img src="man/figures/README-unnamed-chunk-31-1.png" width="100%" />

## **Case study: Data-driven legends**

Suppose we want to change the fill legend key from squares to the actual
violins drawn for each category:

violin_plot <- dplyr::starwars %>%
filter(species == "Human", !is.na(height)) %>%
ggplot(aes(gender, height)) +
geom_violin(aes(fill = gender)) +
theme(
legend.key.size = unit(1, "in"),
legend.key = element_rect(color = "grey", fill = NA)
)

violin_plot

<img src="man/figures/README-unnamed-chunk-32-1.png" width="100%" />

We know that each violin is drawn by `GeomViolin$draw_group`, called for
each group in the plot.

ggtrace_inspect_n(violin_plot, GeomViolin$draw_group)
#> [1] 2

Let’s grab the plotted violins with `ggtrace_inspect_return()`

violins <- lapply(1:2, ggtrace_inspect_return, x = violin_plot, method = GeomViolin$draw_group)
violins
#> [[1]]
#> polygon[geom_violin.polygon.2816]
#>
#> [[2]]
#> polygon[geom_violin.polygon.2880]

Then, we can use `ggtrace_highjack_return()` to highjack the return
value of the key drawing method `GeomViolin$draw_key` such that it
returns the output from the `draw_group` method instead. We set
`cond = 1:2` and value to `substitute(violins[[._counter_]])` so that
the `draw_key` method returns the first violin as the first legend key,
and the second violin as the second legend key.

ggtrace_highjack_return(
violin_plot,
GeomViolin$draw_key,
cond = 1:2,
value = substitute(violins[[._counter_]])
)

<img src="man/figures/README-unnamed-chunk-35-1.png" width="100%" />

Note that violins are drawn with respect to the panel coordinate system,
because that’s how the `draw_group` method does things. We create and
use the function `center_grob()` to center them before they’re passed to
the `value` argument of `ggtrace_highjack_return`.

library(grid)
center_grob <- function(grob) {
x_range <- range(unclass(grob$x))
y_range <- range(unclass(grob$y))
editGrob(grob, vp = viewport(
xscale = x_range, yscale = y_range,
width = diff(x_range), height = diff(y_range)
))
}
violins_stretched <- lapply(violins, center_grob)

ggtrace_highjack_return(
violin_plot,
GeomViolin$draw_key,
cond = 1:2,
value = substitute(violins_stretched[[._counter_]])
)

<img src="man/figures/README-unnamed-chunk-36-1.png" width="100%" />
2 changes: 1 addition & 1 deletion docs/404.html

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

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

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

2 changes: 1 addition & 1 deletion docs/LICENSE.html

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

16 changes: 8 additions & 8 deletions docs/articles/FAQ.html

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

2 changes: 1 addition & 1 deletion docs/articles/casestudy-after_scale.html

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

4 changes: 2 additions & 2 deletions docs/articles/casestudy-ggxmean.html

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

2 changes: 1 addition & 1 deletion docs/articles/comparisons.html

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

2 changes: 1 addition & 1 deletion docs/articles/index.html

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

2 changes: 1 addition & 1 deletion docs/articles/intro.html

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

2 changes: 1 addition & 1 deletion docs/articles/showcase-aes_evaluation.html

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

2 changes: 1 addition & 1 deletion docs/articles/showcase-ggplot_build.html

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

2 changes: 1 addition & 1 deletion docs/articles/technical-details.html

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

Loading

0 comments on commit ba96242

Please sign in to comment.