I am trying to call a plotting function on subgroups of a data.frame with dplyr::do()
, producing one figure (ggplot object) per subgroup. I
want the title of each figure based on the grouping variable. To do this, my function needs to know what the grouping variable is.
Currently, what gets passed to do()
as .
is an object of class tbl_df
and data.frame
. Without explicitly passing it as a separate variable, is there a way to inspect the data.frame directly to learn what was the grouping variable(s) is/are?
The solutions posted here calls for explicitly passing (each of) the grouping variables as an additional argument to the function. I'm wondering if there is a a more, elegant and general solution that is scaleable to varying numbers of grouping variables. While in this specific instance i'm interested in plotting, there are other other use cases where I want to know how the subgroups are defined from within the function called on each subgroup.
I don't want to want to guess by looking for columns where the
length(unique(col)) == 1
because that is going to lead to lots of false
positives with my data.
Is there an elegant way to do this?
Here is some sample code to get started.
library(ggplot2)
my_plot <- function(df) {
subgroup_name <- "" # ??
ggplot(aes(cty, hwy)) + geom_point() +
ggtitle(subgroup_name)
}
mpg %>%
group_by(manufacturer) %>%
do(my_plots = my_plot(.))