Skip to content

Commit

Permalink
fix warning message of ggh4x::facetted_pos_scales
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Jun 21, 2024
1 parent 780b588 commit bd1f4e9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 39 deletions.
67 changes: 42 additions & 25 deletions R/gganno.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ gganno_get_order_list <- function(name, axis, call = quote(draw_heatmap_list)) {
#' @importFrom ggplot2 aes
draw_gganno <- function(anno, order_list, heat_matrix, id) {
which <- anno@which
# we always regard matrix row as the observations
matrix <- anno@matrix %||% switch(which,
row = heat_matrix,
column = t(heat_matrix)
Expand Down Expand Up @@ -228,45 +229,61 @@ draw_gganno <- function(anno, order_list, heat_matrix, id) {
if (!inherits(p$facet, "FacetNull")) {
cli::cli_abort("Cannot set facet in {.fn ggfn} (gganno: {id})")
}
if (!inherits(p$coordinates, "CoordCartesian")) {
cli::cli_abort(paste(
"Only {.fn coord_cartesian} can be used",
"in {.fn ggfn} (gganno: {id})"
))
}
# prepare scales --------------------------------------
labels <- rownames(matrix) %||% ggplot2::waiver()
if (which == "row") {
facet_params <- list(
rows = ggplot2::vars(.data$.slice),
scales = "free_y", space = "free_y"
)
scale_fn <- ggplot2::scale_y_continuous
y_scale <- cheat_scales(coords[c(1L, 3:2)], labels,
scale_fn = ggplot2::scale_y_continuous
)
if (!is.null(p$scales$get_scales("y"))) {
cli::cli_warn(
"will omit y-scale for row annotation (gganno: {id})"
)
}
if (!is.null(x_scale <- p$scales$get_scales("x"))) { # from user
# avoid the warning message: Attempting to add facetted x
# scales, while x scales are not free.
facet_params$scales <- "free"
}
# we always omit the position scales
p$scales <- p$scales$non_position_scales()
} else {
facet_params <- list(
cols = ggplot2::vars(.data$.slice),
scales = "free_x", space = "free_x"
)
scale_fn <- ggplot2::scale_x_continuous
x_scale <- cheat_scales(coords[c(1L, 3:2)], labels,
scale_fn = ggplot2::scale_x_continuous
)
if (!is.null(p$scales$get_scales("x"))) {
cli::cli_warn(
"will omit x-scale for column annotation (gganno: {id})"
)
}
if (!is.null(y_scale <- p$scales$get_scales("y"))) { # from user
# avoid the warning message: Attempting to add facetted y
# scales, while y scales are not free.
facet_params$scales <- "free"
}
p$scales <- p$scales$non_position_scales()
}
# prepare scales
labels <- rownames(matrix) %||% ggplot2::waiver()
scales <- cheat_scales(coords[c(1L, 3:2)], labels, scale_fn = scale_fn)
# add scales into ggplot2 object ---------------------
if (with_slice) {
p <- p + do.call(ggplot2::facet_grid, facet_params)
if (which == "row") {
if (!is.null(p$scales$get_scales("y"))) {
cli::cli_warn(
"will omit y-scale for row annotation (gganno: {id})"
)
}
x_scale <- p$scales$get_scales("x") # from user
p$scales <- p$scales$non_position_scales()
p <- p + ggh4x::facetted_pos_scales(x = x_scale, y = scales)
} else {
if (!is.null(p$scales$get_scales("x"))) {
cli::cli_warn(
"will omit x-scale for column annotation (gganno: {id})"
)
}
y_scale <- p$scales$get_scales("y") # from user
p$scales <- p$scales$non_position_scales()
p <- p + ggh4x::facetted_pos_scales(x = scales, y = y_scale)
}
p <- p + ggh4x::facetted_pos_scales(x = x_scale, y = y_scale)
} else {
p <- p + scales[[1L]]
# it's safe to add `NULL` or a `list`
p <- p + x_scale + y_scale
}
if (isTRUE(anno@debug)) {
cli::cli_inform(
Expand Down
6 changes: 6 additions & 0 deletions R/prepare.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ prepare_ggheat <- function(object) {
"Cannot set facet in {.fn ggfn} (ggheat: {object@name})"
)
}
if (!inherits(p$coordinates, "CoordCartesian")) {
cli::cli_abort(paste(
"Only {.fn coord_cartesian} can be used",
"in {.fn ggfn} (ggheat: {object@name})"
))
}
if (!is.null(p$scales$get_scales("x"))) {
cli::cli_warn("will omit x-scale for ggheat: {object@name}")
}
Expand Down
8 changes: 2 additions & 6 deletions README.html

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ columns added:
number.

- `.x`/`.y`: indicating the x-axis (or y-axis) coordinates. Don’t use
`ggplot2::coord_flip` to flip coordinates as it may disrupt internal
operations.
`ggplot2::coord_flip` to flip coordinates as it can disrupt internal
design.

- `.index`: denoting the row index of the original matrix, where rows
are uniformly considered as observations and columns as variables.
Expand Down Expand Up @@ -513,10 +513,6 @@ draw(ggheat(small_mat,
#> ℹ convert simple vector to one-column matrix
#> ℹ convert simple vector to one-column matrix
#> ℹ convert simple vector to one-column matrix
#> Warning: Attempting to add facetted x scales, while x scales are not free.
#> ℹ Try adding `scales = "free_x"` to the facet.
#> Warning: Attempting to add facetted y scales, while y scales are not free.
#> ℹ Try adding `scales = "free_y"` to the facet.
```

<img src="man/figures/README-unnamed-chunk-7-1.png" width="100%" />
Expand Down
4 changes: 2 additions & 2 deletions vignettes/eheat.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ will be converted into a data.frame with another 3 columns added:
number.

* `.x`/`.y`: indicating the x-axis (or y-axis) coordinates. Don't use
`ggplot2::coord_flip` to flip coordinates as it may disrupt internal
operations.
`ggplot2::coord_flip` to flip coordinates as it can disrupt internal
design.

* `.index`: denoting the row index of the original matrix, where rows are
uniformly considered as observations and columns as variables.
Expand Down

0 comments on commit bd1f4e9

Please sign in to comment.