bayesplot
bayesplot copied to clipboard
Functions for relabeling facets
@tjmahr I'm not sure this is the best way to offer this option, so if you have a better idea I'm definitely open to it. This PR adds functions facet_relabel_gg() and facet_vars(). These are intended to help users change facet labels. This is related to #75 and #157, although those issues relate mostly to changing legend labels, for which we will need some other option. This PR just addresses changing facet labels. (This wasn't much work so we can scrap it if it seems just like it's adding unnecessary bloat, since I suppose we could just tell people they need to use the factor labels they want used as facet labels and we won't provide a way to change them after the fact.)
Example
# example_group_data() has factor levels GroupA and GroupB which are used as facet labels
pgroup <- ppc_scatter_avg_grouped(y = example_y_data(), yrep = example_yrep_draws(), group = example_group_data())
# change the plot to have facet labels "apples" and "oranges" instead of "GroupA" and "GroupB"
pgroup <- facet_relabel_gg(pgroup, labels = c("GroupA" = "apples", "GroupB" = "oranges"))
From the proposed new doc:
-
facet_relabel_ggis atypical in that it accepts a ggplot object as input (so it can detect the variable currently used for faceting) and returns a modified version of the same ggplot object as its output (with update facet labels). The...can be use withfacet_relabel_ggto manually pass other arguments to facet_wrap or facet_grid other than "facets" and "scales" arguments, which are automatically inferred from the plot object. -
facet_varstakes a ggplot object as input and returns a character vector indicating the name of the variable(s) bayesplot uses internally to create facets (if any). Iffacet_relabel_ggisn't flexible enough to relabel the facets how you want thenfacet_varsgives you the info you need to addfacet_wrap(orfacet_grid) to the object yourself. The "scales" attribute of the facet_vars result indicates the value bayesplot used for the scales argument to facet_wrap (or facet_grid).