Skip to content

Commit

Permalink
plot_add_scheme: change argument order
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Dec 14, 2024
1 parent 0c2d833 commit b0e626c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions R/align-link.R
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ AlignLinkProto <- ggproto("AlignLinkProto", AlignGg,
finish_plot = function(self, plot, schemes, theme) {
plot <- plot_add_schemes(plot, schemes)
if (inherits(plot, self$class)) {
element <- calc_element(self$element, complete_theme(plot$theme))
element <- calc_element(self$element, plot$theme)
if (inherits(element, "element_blank")) {
class(plot) <- setdiff(class(plot), self$class)
plot$links_data <- NULL
Expand All @@ -317,7 +317,7 @@ AlignLinkProto <- ggproto("AlignLinkProto", AlignGg,
"panel.spacing.y",
"panel.spacing.x"
),
complete_theme(theme)
theme
) %||% unit(0, "mm")
plot$links_data$element <- element
}
Expand Down
15 changes: 8 additions & 7 deletions R/layout-.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,18 @@ methods::setMethod("$", "LayoutProto", function(x, name) {
})

###########################################################
default_layout <- function(layout) {
layout@theme <- default_theme() + layout@theme
default_layout <- function(layout) { # setup default value for the layout
layout@theme <- complete_theme(default_theme() + layout@theme)

# we by default, collect all guides
layout@schemes$scheme_align["guides"] <- list(
.subset2(.subset2(layout@schemes, "scheme_align"), "guides") %|w|% "tlbr"
)

# we by default, use `default_theme()`
layout@schemes$scheme_theme <- update_scheme(
.subset2(layout@schemes, "scheme_theme"),
new_scheme_theme(default_theme())
new_scheme_theme(complete_theme(default_theme()))
)
layout
}
Expand All @@ -91,17 +93,16 @@ inherit_parent_layout_theme <- function(layout, theme, direction) {
if (is.null(theme)) return(layout@theme) # styler: off
# parent theme, set the global panel spacing,
# so that every panel aligns well
if (is.null(layout@theme)) return(theme) # styler: off
if (is_horizontal(direction)) {
theme <- theme(
theme + layout@theme + theme(
panel.spacing.y = calc_element("panel.spacing.y", theme)
)
} else {
theme <- theme(
theme + layout@theme + theme(
panel.spacing.x = calc_element("panel.spacing.x", theme)
)
}
if (is.null(layout@theme)) return(theme) # styler: off
layout@theme + theme
}

############################################################
Expand Down
10 changes: 6 additions & 4 deletions R/scheme-.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ ggalign_scheme_name <- function(x) {
###############################################################
#' Used to update global data
#' @noRd
update_scheme <- function(new, old, object_name) UseMethod("update_scheme")
update_scheme <- function(new, old, object_name) {
UseMethod("update_scheme", old)
}

#' @export
update_scheme.default <- function(new, old, object_name) new
Expand Down Expand Up @@ -64,14 +66,14 @@ inherit_schemes <- function(schemes, pschemes) {
}

###############################################################
plot_add_scheme <- function(scheme, plot) UseMethod("plot_add_scheme", scheme)
plot_add_scheme <- function(plot, scheme) UseMethod("plot_add_scheme", scheme)

#' @export
plot_add_scheme.NULL <- function(scheme, plot) plot
plot_add_scheme.NULL <- function(plot, scheme) plot

plot_add_schemes <- function(plot, schemes) {
for (i in seq_along(schemes)) {
plot <- plot_add_scheme(scheme = .subset2(schemes, i), plot = plot)
plot <- plot_add_scheme(plot, scheme = .subset2(schemes, i))
}
plot
}
2 changes: 1 addition & 1 deletion R/scheme-align.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ inherit_scheme.scheme_align <- function(scheme, pscheme) {

#' @param theme Additional default theme elements to be added for the plot
#' @noRd
plot_add_scheme.scheme_align <- function(scheme, plot) {
plot_add_scheme.scheme_align <- function(plot, scheme) {
if (!is.waive(free_guides <- .subset2(scheme, "guides"))) {
plot <- free_guide(plot, free_guides)
}
Expand Down
2 changes: 1 addition & 1 deletion R/scheme-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ inherit_scheme.scheme_data <- function(scheme, pscheme) {
}

#' @export
plot_add_scheme.scheme_data <- function(scheme, plot) {
plot_add_scheme.scheme_data <- function(plot, scheme) {
# by default, we won't change the data
if (!is.null(scheme_data <- .subset2(scheme, "data") %|w|% NULL) &&
!is.null(raw_data <- .subset2(plot, "data"))) {
Expand Down
4 changes: 1 addition & 3 deletions R/scheme-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ update_scheme.scheme_theme <- function(new, old, object_name) {

#' @export
inherit_scheme.scheme_theme <- function(scheme, pscheme) {
# By default, we'll always complete the theme when building the layout
# so parent always exist.
pscheme + scheme
}

#' @export
plot_add_scheme.scheme_theme <- function(scheme, plot) {
plot_add_scheme.scheme_theme <- function(plot, scheme) {
# setup plot theme
plot$theme <- scheme + .subset2(plot, "theme")
plot
Expand Down

0 comments on commit b0e626c

Please sign in to comment.