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

suggestion to address get_legend issue #205 #206

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ Collate:
'stamp.R'
'themes.R'
'utils_ggplot2.R'
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export(gtable_squash_cols)
export(gtable_squash_rows)
export(insert_xaxis_grob)
export(insert_yaxis_grob)
export(is_zeroGrob)
export(panel_border)
export(pdf_null_device)
export(plot_component_names)
Expand Down
4 changes: 2 additions & 2 deletions R/get_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
#' plot_grid(NULL, legend, ncol=1),
#' rel_widths=c(1, 0.2)))
#' @export
get_legend <- function(plot) {
get_plot_component(plot, "guide-box")
get_legend <- function(plot, return_all = FALSE) {
get_plot_component(plot, "guide-box", return_all = return_all)
}
27 changes: 25 additions & 2 deletions R/get_plot_component.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
#' is zero grob
#'
#' Function to check if a grob is a zero grob
#'
#' @param x A grob
#' @return A logical value
#' @export
is_zeroGrob <- function(x) {
"zeroGrob" %in% attr(x, "class")
}


#' Get plot components
#'
#' Extract plot components from a ggplot or gtable. `get_plot_component()`
Expand All @@ -18,6 +30,9 @@
#' p <- ggplot(mpg, aes(displ, cty)) + geom_point()
#' ggdraw(get_plot_component(p, "ylab-l"))
#'
#' plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size=2.5) + theme(legend.position='bottom')
#' ggdraw(get_plot_component(plot.mpg, "guide-box"))
#'
#' @export
get_plot_component <- function(plot, pattern, return_all = FALSE) {
plot <- as_gtable(plot)
Expand All @@ -30,8 +45,16 @@ get_plot_component <- function(plot, pattern, return_all = FALSE) {
if (length(grobIndex) > 1 && !return_all) {
# If there's more than one grob, return just the first one
warning("Multiple components found; returning the first one. To return all, use `return_all = TRUE`.")
grobIndex <- grobIndex[1]
matched_grobs <- grobs[[grobIndex]]
#grobIndex <- grobIndex[1]
matched_grobs1 <- grobs[grobIndex]
# Remove zero grobs
matched_grobs_non_zero <- matched_grobs1[!sapply(matched_grobs1, is_zeroGrob)]
# now get the first grob
if(length(matched_grobs_non_zero) == 0) {
matched_grobs <- NULL
} else {
matched_grobs <- matched_grobs_non_zero[[1]]
}
} else if (length(grobIndex) > 1 && return_all) {
# If there's more than one grob, return all as a list
matched_grobs <- grobs[grobIndex]
Expand Down
2 changes: 1 addition & 1 deletion man/get_legend.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/get_plot_component.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/is_zeroGrob.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions tests/testthat/_snaps/draw_/bottom-right-half-width-height.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions tests/testthat/_snaps/draw_/centered-full-width-height.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions tests/testthat/_snaps/draw_/centered-half-width-height.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions tests/testthat/_snaps/draw_/four-corners-aligned-scaled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions tests/testthat/_snaps/draw_/four-corners-centered-scaled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading