Skip to content

Commit

Permalink
remove much differences
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Sep 6, 2024
1 parent f4f7b3f commit 8b98877
Showing 1 changed file with 55 additions and 63 deletions.
118 changes: 55 additions & 63 deletions R/guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,59 +60,6 @@ collapse_guides <- function(guides) {
guides
}

#' @importFrom utils getFromNamespace
#' @importFrom ggplot2 calc_element
assemble_guides <- function(guides, position, theme) {
# https://github.com/tidyverse/ggplot2/blob/57ba97fa04dadc6fd73db1904e39a09d57a4fcbe/R/guides-.R#L512
theme$legend.spacing <- theme$legend.spacing %||% unit(0.5, "lines")
theme$legend.spacing.y <- calc_element("legend.spacing.y", theme)
theme$legend.spacing.x <- calc_element("legend.spacing.x", theme)

# for every position, collect all individual guides and arrange them
# into a guide box which will be inserted into the main gtable
Guides <- getFromNamespace("Guides", "ggplot2")
package_box <- .subset2(Guides, "package_box") %||% package_box
package_box(guides, position, theme)
}

#' @importFrom grid valid.just editGrob
package_box <- function(guides, guide_pos, theme) {
theme <- complete_guide_theme(guide_pos, theme)
guides <- guides_build(guides, theme)

# Set the justification of the legend box
# First value is xjust, second value is yjust
just <- valid.just(calc_element("legend.justification", theme))
xjust <- just[1]
yjust <- just[2]
guides <- editGrob(guides,
vp = viewport(x = xjust, y = yjust, just = c(xjust, yjust))
)
guides <- gtable_add_rows(guides, unit(yjust, "null"))
guides <- gtable_add_rows(guides, unit(1 - yjust, "null"), 0)
guides <- gtable_add_cols(guides, unit(xjust, "null"), 0)
guides <- gtable_add_cols(guides, unit(1 - xjust, "null"))
guides
}

#' @importFrom ggplot2 calc_element
complete_guide_theme <- function(guide_pos, theme) {
if (guide_pos %in% c("top", "bottom")) {
theme$legend.box <- calc_element("legend.box", theme) %||% "horizontal"
theme$legend.direction <- calc_element("legend.direction", theme) %||%
"horizontal"
theme$legend.box.just <- calc_element("legend.box.just", theme) %||%
c("center", "top")
} else {
theme$legend.box <- calc_element("legend.box", theme) %||% "vertical"
theme$legend.direction <- calc_element("legend.direction", theme) %||%
"vertical"
theme$legend.box.just <- calc_element("legend.box.just", theme) %||%
c("left", "top")
}
theme
}

#' @importFrom gtable gtable_width gtable_height gtable gtable_add_grob
#' @importFrom grid editGrob heightDetails widthDetails valid.just unit.c unit
#' @importFrom ggplot2 margin element_grob element_blank calc_element element_render
Expand All @@ -129,26 +76,24 @@ guides_build <- function(guides, theme) {
yjust <- just[2]
vert <- identical(calc_element("legend.box", theme), "horizontal")
guides <- lapply(guides, function(g) {
editGrob(g, vp = viewport(
x = xjust, y = yjust, just = c(xjust, yjust),
height = if (vert) heightDetails(g) else 1,
width = if (!vert) widthDetails(g) else 1
))
editGrob(g, vp = viewport(x = xjust, y = yjust, just = c(xjust, yjust),
height = if (vert) heightDetails(g) else 1,
width = if (!vert) widthDetails(g) else 1))
})
guide_ind <- seq(by = 2, length.out = length(guides))
sep_ind <- seq(2, by = 2, length.out = length(guides) - 1)
if (vert) {
heights <- max(heights)
if (length(widths) != 1) {
w <- unit(rep_len(0, length(widths) * 2 - 1), "mm")
w <- unit(rep_len(0, length(widths) * 2 - 1), 'mm')
w[guide_ind] <- widths
w[sep_ind] <- legend.spacing.x
widths <- w
}
} else {
widths <- max(widths)
if (length(heights) != 1) {
h <- unit(rep_len(0, length(heights) * 2 - 1), "mm")
h <- unit(rep_len(0, length(heights) * 2 - 1), 'mm')
h[guide_ind] <- heights
h[sep_ind] <- legend.spacing.y
heights <- h
Expand All @@ -157,11 +102,11 @@ guides_build <- function(guides, theme) {
widths <- unit.c(legend.box.margin[4], widths, legend.box.margin[2])
heights <- unit.c(legend.box.margin[1], heights, legend.box.margin[3])
guides <- gtable_add_grob(
gtable(widths, heights, name = "guide-box"),
gtable(widths, heights, name = 'guide-box'),
guides,
t = 1 + if (!vert) guide_ind else 1,
l = 1 + if (vert) guide_ind else 1,
name = "guides"
name = 'guides'
)

gtable_add_grob(
Expand All @@ -171,6 +116,53 @@ guides_build <- function(guides, theme) {
z = -Inf, clip = "off", name = "legend.box.background"
)
}
#' @importFrom ggplot2 calc_element
complete_guide_theme <- function(guide_pos, theme) {
if (guide_pos %in% c("top", "bottom")) {
theme$legend.box <- theme$legend.box %||% "horizontal"
theme$legend.direction <- theme$legend.direction %||% "horizontal"
theme$legend.box.just <- theme$legend.box.just %||% c("center", "top")
} else {
theme$legend.box <- theme$legend.box %||% "vertical"
theme$legend.direction <- theme$legend.direction %||% "vertical"
theme$legend.box.just <- theme$legend.box.just %||% c("left", "top")
}
theme
}
#' @importFrom utils getFromNamespace
#' @importFrom ggplot2 calc_element
assemble_guides <- function(guides, position, theme) {
# https://github.com/tidyverse/ggplot2/blob/57ba97fa04dadc6fd73db1904e39a09d57a4fcbe/R/guides-.R#L512
theme$legend.spacing <- theme$legend.spacing %||% unit(0.5, "lines")
theme$legend.spacing.y <- calc_element("legend.spacing.y", theme)
theme$legend.spacing.x <- calc_element("legend.spacing.x", theme)

# for every position, collect all individual guides and arrange them
# into a guide box which will be inserted into the main gtable
Guides <- getFromNamespace("Guides", "ggplot2")
package_box <- .subset2(Guides, "package_box") %||% package_box
package_box(guides, position, theme)
}

#' @importFrom grid valid.just editGrob
package_box <- function(guides, guide_pos, theme) {
theme <- complete_guide_theme(guide_pos, theme)
guides <- guides_build(guides, theme)

# Set the justification of the legend box
# First value is xjust, second value is yjust
just <- valid.just(calc_element("legend.justification", theme))
xjust <- just[1]
yjust <- just[2]
guides <- editGrob(guides,
vp = viewport(x = xjust, y = yjust, just = c(xjust, yjust))
)
guides <- gtable_add_rows(guides, unit(yjust, 'null'))
guides <- gtable_add_rows(guides, unit(1 - yjust, 'null'), 0)
guides <- gtable_add_cols(guides, unit(xjust, 'null'), 0)
guides <- gtable_add_cols(guides, unit(1 - xjust, 'null'))
guides
}

#' @importFrom ggplot2 calc_element find_panel
#' @importFrom gtable gtable_width gtable_height
Expand All @@ -186,7 +178,7 @@ attach_guides <- function(table, guides, position, theme) {
return(table)
}
p_loc <- find_panel(table)
spacing <- calc_element("legend.box.spacing", theme) %||% unit(0.2, "cm")
spacing <- calc_element("legend.box.spacing", theme) %||% unit(0.2, 'cm')
legend_width <- gtable_width(guides)
legend_height <- gtable_height(guides)
if (position == "left") {
Expand Down

0 comments on commit 8b98877

Please sign in to comment.