Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename geom_connect() into geom_connector() #82

Merged
merged 2 commits into from
Dec 28, 2024
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
8 changes: 4 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

S3method(round_any,POSIXct)
S3method(round_any,numeric)
export(GeomConnect)
export(GeomConnector)
export(PositionDiverging)
export(PositionLikert)
export(StatCross)
Expand All @@ -11,14 +11,14 @@ export(StatWeightedMean)
export(augment_chisq_add_phi)
export(auto_contrast)
export(compute_cascade)
export(geom_connect)
export(geom_connect_bars)
export(geom_bar_connector)
export(geom_connector)
export(geom_diverging)
export(geom_diverging_text)
export(geom_likert)
export(geom_likert_text)
export(geom_prop_bar)
export(geom_prop_connect)
export(geom_prop_connector)
export(geom_prop_text)
export(geom_pyramid)
export(geom_pyramid_text)
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

**Improvements**

* New geoms `geom_connect()`, `geom_connect_bars()` and
`geom_prop_connect()` (#81)
* New geoms `geom_connector()`, `geom_bar_connector()` and
`geom_prop_connector()` (#81)
* New shortcut `auto_contrast` (#75)

# ggstats 0.7.0
Expand Down
91 changes: 46 additions & 45 deletions R/geom_connect.R → R/geom_connector.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#' Connect bars / points
#'
#' `geom_connect()` is a variation of [ggplot2::geom_step()] adapted to
#' connect bars, but could also be used to connect points.
#' `geom_connector()` is a variation of [ggplot2::geom_step()].
#' Its variant `geom_bar_connector()` is particularly adapted to
#' connect bars.
#'
#' @inheritParams ggplot2::geom_step
#' @param width Bar width (see examples).
Expand All @@ -11,20 +12,20 @@
#' @examples
#' library(ggplot2)
#'
#' # geom_connect_bars() -----------
#' # geom_bar_connector() -----------
#'
#' ggplot(diamonds) +
#' aes(x = clarity, fill = cut) +
#' geom_bar(width = .5) +
#' geom_connect_bars(width = .5, linewidth = .25) +
#' geom_bar_connector(width = .5, linewidth = .25) +
#' theme_minimal() +
#' theme(legend.position = "bottom")
#'
#' \donttest{
#' ggplot(diamonds) +
#' aes(x = clarity, fill = cut) +
#' geom_bar(width = .5) +
#' geom_connect_bars(
#' geom_bar_connector(
#' width = .5,
#' continuous = TRUE,
#' colour = "red",
Expand All @@ -36,63 +37,63 @@
#' ggplot(diamonds) +
#' aes(x = clarity, fill = cut) +
#' geom_bar(width = .5, position = "fill") +
#' geom_connect_bars(width = .5, position = "fill") +
#' geom_bar_connector(width = .5, position = "fill") +
#' theme(legend.position = "bottom")
#'
#' ggplot(diamonds) +
#' aes(x = clarity, fill = cut) +
#' geom_bar(width = .5, position = "diverging") +
#' geom_connect_bars(width = .5, position = "diverging", linewidth = .25) +
#' geom_bar_connector(width = .5, position = "diverging", linewidth = .25) +
#' theme(legend.position = "bottom")
#'
#' # geom_connect() -----------
#' # geom_connector() -----------
#'
#' ggplot(mtcars) +
#' aes(x = wt, y = mpg, colour = factor(cyl)) +
#' geom_connect() +
#' geom_connector() +
#' geom_point()
#'
#' ggplot(mtcars) +
#' aes(x = wt, y = mpg, colour = factor(cyl)) +
#' geom_connect(continuous = TRUE) +
#' geom_connector(continuous = TRUE) +
#' geom_point()
#'
#' ggplot(mtcars) +
#' aes(x = wt, y = mpg, colour = factor(cyl)) +
#' geom_connect(continuous = TRUE, width = .3) +
#' geom_connector(continuous = TRUE, width = .3) +
#' geom_point()
#'
#' ggplot(mtcars) +
#' aes(x = wt, y = mpg, colour = factor(cyl)) +
#' geom_connect(width = 0) +
#' geom_connector(width = 0) +
#' geom_point()
#'
#' ggplot(mtcars) +
#' aes(x = wt, y = mpg, colour = factor(cyl)) +
#' geom_connect(width = Inf) +
#' geom_connector(width = Inf) +
#' geom_point()
#'
#' ggplot(mtcars) +
#' aes(x = wt, y = mpg, colour = factor(cyl)) +
#' geom_connect(width = Inf, continuous = TRUE) +
#' geom_connector(width = Inf, continuous = TRUE) +
#' geom_point()
#' }
geom_connect <- function(mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
width = 0.1,
continuous = FALSE,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE,
...) {
geom_connector <- function(mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
width = 0.1,
continuous = FALSE,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE,
...) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomConnect,
geom = GeomConnector,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
Expand All @@ -106,20 +107,20 @@ geom_connect <- function(mapping = NULL,
)
}

#' @rdname geom_connect
#' @rdname geom_connector
#' @export
geom_connect_bars <- function(mapping = NULL,
data = NULL,
stat = "prop",
position = "stack",
width = 0.9,
continuous = FALSE,
add_baseline = TRUE,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE,
...) {
geom_bar_connector <- function(mapping = NULL,
data = NULL,
stat = "prop",
position = "stack",
width = 0.9,
continuous = FALSE,
add_baseline = TRUE,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE,
...) {
params <- rlang::list2(
width = width,
continuous = continuous,
Expand All @@ -135,20 +136,20 @@ geom_connect_bars <- function(mapping = NULL,
data = data,
mapping = mapping,
stat = stat,
geom = GeomConnect,
geom = GeomConnector,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = params
)
}

#' @rdname geom_connect
#' @rdname geom_connector
#' @format NULL
#' @usage NULL
#' @export
GeomConnect <- ggproto(
"GeomConnect",
GeomConnector <- ggproto(
"GeomConnector",
ggplot2::GeomPath,
setup_params = function(data, params) {
params$flipped_aes <- has_flipped_aes(data, params, ambiguous = TRUE)
Expand Down Expand Up @@ -203,8 +204,8 @@ GeomConnect <- ggproto(
}
)

#' Calculate connections for `geom_connect()`
#' Used by `GeomConnect()`
#' Calculate connections for `geom_connector()`
#' Used by `GeomConnector()`
#'
#' @noRd
connect_points <- function(data, width = 0.9, continuous = FALSE) {
Expand Down
30 changes: 15 additions & 15 deletions R/stat_prop.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,32 +214,32 @@ StatProp <- ggplot2::ggproto("StatProp", ggplot2::Stat,

#' Convenient geometries for proportion bar plots
#'
#' `geom_prop_bar()`, `geom_prop_text()` and `geom_prop_connect()` are
#' `geom_prop_bar()`, `geom_prop_text()` and `geom_prop_connector()` are
#' variations of [ggplot2::geom_bar()], [ggplot2::geom_text()] and
#' [geom_connect_bars()] using [stat_prop()], with custom default aesthetics:
#' [geom_bar_connector()] using [stat_prop()], with custom default aesthetics:
#' `after_stat(prop)` for **x** or **y**, and
#' `scales::percent(after_stat(prop))` for **label**.
#'
#' @inheritParams stat_prop
#' @param width Bar width (`0.9` by default).
#' @param ... Additional parameters passed to [ggplot2::geom_bar()],
#' [ggplot2::geom_text()] or [geom_connect_bars()].
#' [ggplot2::geom_text()] or [geom_bar_connector()].
#' @export
#' @seealso [geom_connect_bars()]
#' @seealso [geom_bar_connector()]
#' @examples
#' library(ggplot2)
#' d <- as.data.frame(Titanic)
#' ggplot(d) +
#' aes(x = Class, fill = Survived, weight = Freq) +
#' geom_prop_bar() +
#' geom_prop_text() +
#' geom_prop_connect()
#' geom_prop_connector()
#'
#' ggplot(d) +
#' aes(y = Class, fill = Survived, weight = Freq) +
#' geom_prop_bar(width = .5) +
#' geom_prop_text() +
#' geom_prop_connect(width = .5, linetype = "dotted")
#' geom_prop_connector(width = .5, linetype = "dotted")
#'
#' ggplot(d) +
#' aes(
Expand All @@ -251,7 +251,7 @@ StatProp <- ggplot2::ggproto("StatProp", ggplot2::Stat,
#' ) +
#' geom_prop_bar() +
#' geom_prop_text() +
#' geom_prop_connect()
#' geom_prop_connector()
geom_prop_bar <- function(mapping = NULL,
data = NULL,
position = "stack",
Expand Down Expand Up @@ -293,14 +293,14 @@ geom_prop_text <- function(mapping = ggplot2::aes(!!!auto_contrast),

#' @rdname geom_prop_bar
#' @export
geom_prop_connect <- function(mapping = NULL,
data = NULL,
position = "stack",
...,
width = 0.9,
complete = "fill",
default_by = "x") {
geom_connect_bars(
geom_prop_connector <- function(mapping = NULL,
data = NULL,
position = "stack",
...,
width = 0.9,
complete = "fill",
default_by = "x") {
geom_bar_connector(
mapping = mapping,
data = data,
position = position,
Expand Down
16 changes: 14 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,22 @@ df <-
gglikert(df)
```

## Cascade plot (*experimental*)
## Connect bars

```{r}
ggplot2::diamonds |>
ggplot(diamonds) +
aes(x = clarity, fill = cut) +
geom_bar(width = .5) +
geom_bar_connector(width = .5, linewidth = .25) +
theme_minimal() +
theme(legend.position = "bottom")
```


## Cascade plot

```{r}
diamonds |>
ggcascade(
all = TRUE,
big = carat > .5,
Expand Down
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,28 @@ gglikert(df)

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

## Cascade plot (*experimental*)
## Connect bars

``` r
ggplot2::diamonds |>
ggplot(diamonds) +
aes(x = clarity, fill = cut) +
geom_bar(width = .5) +
geom_bar_connector(width = .5, linewidth = .25) +
theme_minimal() +
theme(legend.position = "bottom")
```

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

## Cascade plot

``` r
diamonds |>
ggcascade(
all = TRUE,
big = carat > .5,
"big & ideal" = carat > .5 & cut == "Ideal"
)
```

<img src="man/figures/README-unnamed-chunk-11-1.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-12-1.png" width="100%" />
2 changes: 1 addition & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ reference:

- title: Additional geometries for `ggplot2`
contents:
- geom_connect
- geom_connector
- geom_stripped_rows

- title: Convenient geometries / layers
Expand Down
Binary file modified man/figures/README-unnamed-chunk-10-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-11-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/figures/README-unnamed-chunk-12-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-4-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-5-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-6-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-7-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-9-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading