Replies: 3 comments 14 replies
-
Just commenting on the error message:
This is because you have |
Beta Was this translation helpful? Give feedback.
-
I think this is a really interesting question. Something that's bothered me with ggplot2 for quite awhile is that it doesn't have a formal notion of a vector of layers (which kind of relates to #66), which would be the obvious solution here in my opinion. The lack of a "vector of layers" means that you get surprising results when you do things like add two layers together without a plot. E.g. take the following: plot = ggplot()
layer1 = geom_point(data = mtcars, aes(mpg, mpg))
layer2 = geom_point(aes(cty, hwy), data = mpg, color = "blue") The combined plot is: plot + layer1 + layer2 We expect plot + (layer1 + layer2)
## Error in `+.gg`:
## ! Cannot add <ggproto> objects together.
## ℹ Did you forget to add this object to a <ggplot> object?
## Run `rlang::last_trace()` to see where the error occurred. Because The existing solution is to explicitly create a list of layers, which acts sort of like a layer vector, a la |
Beta Was this translation helpful? Give feedback.
-
Somehow, this got me thinking about using patchwork with ggplot_add data_filter() #74 and then later aes() to get some in-paradigm 'crosstalk'-ish behavior, and learned about the * and & operators for applying new adds to patchwork plot ensembles. Very exciting! 🤯 Though library(ggplot2)
data_filter <- function(.keep, .by) {
structure(list(keep_specification = rlang::enquo(.keep),
by_specification = rlang::enquo(.by)),
class = "filterobs")
}
ggplot_add.filterobs <- function(object, plot, object_name) {
new_data <- dplyr::filter(plot$data,
!!object$keep_specification,
.by = !!object$by_specification)
plot$data <- new_data
plot
}
mtcars
plot(mtcars)
ggplot(mtcars) +
aes(factor(cyl), fill = factor(cyl)) +
geom_bar() -> p1;p1
ggplot(mtcars) +
aes(id = cyl, fill = factor(cyl)) +
ggcirclepack::geom_circlepack() +
coord_equal() -> p2
ggplot(mtcars) +
aes(wt, mpg, color = factor(cyl)) +
geom_point(size = 4) -> p3
library(patchwork)
p1 + p2 + p3 (p1 + p2 + p3) * data_filter(cyl != 4) (p1 + p2 + p3) * aes(alpha = cyl != 4) The patchwork <- p3 / (p1 | p2)
patchwork & theme_minimal() And seems to work as advertised w/ aes, but not data_filter() patchwork & aes(alpha = cyl == 4) & guides(alpha = "none") patchwork & data_filter(cyl != 4) Error in UseMethod("filter"): no applicable method for 'filter' applied to an object of class "waiver" |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I think another advent question might about reusing layers that aren't fully locally defined. So you can grab layers from p1 and p2 below, which have layer information all local, and apply those layers in a new plot... (kind of inspired by the composition of swim plots in @rsh52's work - there are three layers and you might want to look at those in separate plots, and then superimpose them later)
But using a more common plot building approach, we cannot just use the layer directly.
I think target might be add_layers() (via ggplot_add, but I haven't actually attempted).
Beta Was this translation helpful? Give feedback.
All reactions