Skip to content

Commit

Permalink
internal changes: add_default_mapping accept mapping directly instead…
Browse files Browse the repository at this point in the history
… of plot
  • Loading branch information
Yunuuuu committed Oct 16, 2024
1 parent 059b1b9 commit 00a9cbb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
7 changes: 5 additions & 2 deletions R/align-dendrogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,11 @@ AlignDendro <- ggproto("AlignDendro", Align,
return(NULL)
}
direction <- .subset2(self, "direction")
ans <- ggplot2::ggplot(mapping = mapping)
add_default_mapping(ans, aes(x = .data$x, y = .data$y)) +
ggplot2::ggplot(
mapping = add_default_mapping(
mapping, aes(x = .data$x, y = .data$y)
)
) +
rlang::inject(ggplot2::geom_segment(
mapping = aes(
x = .data$x, y = .data$y,
Expand Down
14 changes: 7 additions & 7 deletions R/align-gg.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ AlignGG <- ggproto("AlignGG", Align,
},
ggplot = function(self, mapping) {
direction <- .subset2(self, "direction")
ans <- ggplot2::ggplot(mapping = mapping)

add_default_mapping(ans, switch_direction(
direction,
aes(y = .data$.y),
aes(x = .data$.x)
))
ggplot2::ggplot(
mapping = add_default_mapping(mapping, switch_direction(
direction,
aes(y = .data$.y),
aes(x = .data$.x)
))
)
},

#' @importFrom vctrs vec_expand_grid vec_cbind
Expand Down
14 changes: 7 additions & 7 deletions R/align-panel.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ AlignPanel <- ggproto("AlignPanel", Align,
},
ggplot = function(self, mapping) {
direction <- .subset2(self, "direction")
ans <- ggplot2::ggplot(mapping = mapping) +
ggplot2::ggplot(
mapping = add_default_mapping(mapping, switch_direction(
direction,
aes(y = .data$.y),
aes(x = .data$.x)
))
) +
# remove the title and text of axis vertically with the layout
switch_direction(
direction,
Expand All @@ -79,12 +85,6 @@ AlignPanel <- ggproto("AlignPanel", Align,
axis.ticks.y = element_blank()
)
)

add_default_mapping(ans, switch_direction(
direction,
aes(y = .data$.y),
aes(x = .data$.x)
))
},
draw = function(self, panel, index, extra_panel, extra_index) {
axis <- to_coord_axis(.subset2(self, "direction"))
Expand Down
5 changes: 3 additions & 2 deletions R/layout-heatmap-.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ heatmap_layout.default <- function(data, ...) {
assert_layout_position(guides, call = call)
action$guides <- guides
}
plot <- ggplot2::ggplot(mapping = mapping)
plot <- add_default_mapping(plot, aes(.data$.x, .data$.y)) +
plot <- ggplot2::ggplot(
mapping = add_default_mapping(mapping, aes(.data$.x, .data$.y))
) +
# always remove default axis titles -------------------
# https://stackoverflow.com/questions/72402570/why-doesnt-gplot2labs-overwrite-update-the-name-argument-of-scales-function
# There are multiple ways to set labels in a plot, which take different
Expand Down
6 changes: 2 additions & 4 deletions R/utils-ggplot2.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ is.waive <- function(x) inherits(x, "waiver")

snake_class <- function(x) ggfun("snake_class")(x)

add_default_mapping <- function(plot, default_mapping) {
mapping <- .subset2(plot, "mapping")
add_default_mapping <- function(mapping, default_mapping) {
for (nm in names(mapping)) {
default_mapping[[nm]] <- .subset2(mapping, nm)
}
plot$mapping <- default_mapping
plot
default_mapping
}

#' @importFrom rlang env_clone
Expand Down

0 comments on commit 00a9cbb

Please sign in to comment.