-
Notifications
You must be signed in to change notification settings - Fork 105
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
Enable Operation on Lists of Plots #186
Comments
I kept bashing my head against it a bit longer, and I came up with an implementation that will work if you use library(ggplot2)
library(ggforce)
as_gg_list <- function(x) {
UseMethod("as_gg_list")
}
as_gg_list.gg <- function(x) {
if (is.null(n_pages(x))) {
as_gg_list(list(x))
} else {
ret <- list()
for (idx in seq_len(n_pages(x))) {
new_facet <-
ggproto(
NULL, FacetWrapPaginate,
shrink = x$facet$shrink,
params = c(
x$facet$params[setdiff(names(x$facet$params), "page")],
list(page = idx)
)
)
ret[[idx]] <- x + new_facet
}
as_gg_list(ret)
}
}
as_gg_list.list <- function(x) {
class(x) <- c("gg_list", "gg")
x
}
print.gg_list <- function(x, ..., fig_suffix = "\n\n") {
for (idx in seq_along(x)) {
print(x[[idx]])
cat(fig_suffix)
}
}
`%+%` <- function(e1, e2) {
UseMethod("%+%")
}
`%+%.gg_list` <- function(e1, e2) {
as_gg_list(
lapply(X=e1, FUN="+", e2)
)
}
`%+%.default` <- function(e1, e2) {
ggplot2::`%+%`(e1, e2)
}
ggplot.list <- function(data=NULL, mapping = aes(), ..., environment = parent.frame()) {
as_gg_list(
lapply(
X=data,
FUN=ggplot,
mapping=mapping,
...,
environment=environment
)
)
}
d_list <- split(diamonds, f=diamonds$color)
p <-
ggplot(diamonds, aes(x=carat, y=price)) +
geom_hex() +
facet_wrap_paginate(facets="color", nrow=1, ncol=1)
n_pages(p)
p_list <- as_gg_list(p)
p_list_2 <- p_list %+% scale_y_log10()
p_list_3 <-
ggplot(d_list, aes(x=carat, y=price)) %+%
geom_hex() %+%
facet_wrap(~color) |
I don't think this is a good fit for ggforce, as the focus is to provide new geoms/stats/facets etc, not add a new operator |
In case someone else has this use case, I'm about to release the (I agree that it's not a good fit for |
As described in #93, I often operate on many plots that have similar features. I would love it if there were the ability to work with a list of plots as though they were a single plot. I could imagine that this would dovetail in with the request in #7 to print all pages in
facet_wrap_paginate()
orfacet_grid_paginate()
. What I'm thinking of would look something like the following, but I can't find a way to make it work with a simple+
instead of%+list%
:The text was updated successfully, but these errors were encountered: