Home > database >  Passing column names to labeller in a custom facet_grid function
Passing column names to labeller in a custom facet_grid function

Time:01-24

I'm trying to create a function to create a customized version of a facet_grid plot. I'd like to be able to specify the dataframe, the names of several columns to be used as x/y and faceting variables, and then some customized labels. I'm able to successfully create the facet_grid function, but I'm having trouble with the labeller. I'm passing the names of the columns to be used as faceting variables into the function as strings, but I'm not sure how to get the labeller to recognize the column names.

Here is an example. First, this is code for the plot I'd like to recreate using a custom function:

library(dplyr)
library(ggplot2)

#sample data
data(msleep)
filtered<- msleep %>% filter(conservation %in% c('en', 'vu', 'domesticated'))

#readable labels
fancy_labels_cons <- c('domesticated' = 'Domesticated', 'en' = 'Endangered', 'vu' = 'Vulnerable')
fancy_labels_vore <- c('carni' = 'Carnivore', 'herbi'='Herbivore', 'omni'='Omnivore', 'insecti'='Insectivore')

#plot using facet grid with labels
ggplot(filtered, aes(x = sleep_total, y = awake))   
  geom_point()  
  facet_grid(vore ~ conservation, labeller = labeller(conservation=fancy_labels_cons, vore=fancy_labels_vore))

Which gives the desired result, using the fancy labels for both faceting variables: Facet_grid of conservation and vore

To turn it into a function, I pass the column names as strings. Facet_grid didn't recognize multiple variables using the .data[[str]] syntax, so I created a formula. However, the labeller doesn't recognize the column names (facetx and facety), unless I specify them manually. How can the column name arguments be used to specify the different variable labels within the function?

myfunc <- function(df, x, y, facetx, facety, xlabs, ylabs){
  #convert column-name strings to formula
  facet_func <- as.formula(paste0(facetx, ' ~ ', facety))
  ggplot(df, aes(.data[[x]], .data[[y]]))   
    geom_point()  
    facet_grid(facet_func, 
               #this line doesn't work. Using .data[[facetx]] creates an error
               labeller = labeller(facetx=fancy_labels_cons, facety=fancy_labels_vore))
}

myfunc(filtered, x='sleep_total', y='awake', facetx='conservation', facety='vore', 
       fancy_labels_cons, fancy_labels_vore)

CodePudding user response:

@RitchieSacramento has identified the core of the problem. You can read more about NSE enter image description here

Note that I've taken certain liberties with the labeller as I didn't have time to figure out a robust way of labelling two different dimensions that might contain common values that require different labels in each dimension.

If Ritchie also posts an answer, his has precedence. Please accept his.

  • Related