Passing argument to facet grid in function -ggplot

I am trying to write a function to plot graphs in a grid. I am using ggplot and facet grid. I am unable to pass the argument for facet grid. I wonder if anybody can point me in the right direction.

The data example:

 Year = as.factor(rep(c("01", "02"), each = 4, times = 1))
 Group = as.factor(rep(c("G1", "G2"), each = 2, times = 2))
 Gender = as.factor(rep(c("Male", "Female"),   times = 4))
 Percentage = as.integer(c("80","20","50","50","45","55","15","85"))
 df1 = data.frame (Year, Group, Gender, Percentage)

The code for the grid plot without function is:

p = ggplot(data=df1, aes(x=Year, y=Percentage, fill = Gender)) + geom_bar(stat = "identity")
p = p +  facet_grid(~ Group, scales = 'free')  
p

This produces a plot like the ones I want to do. However, when I put it into a function:

MyGridPlot <- function (df, x_axis, y_axis, bar_fill, fgrid){
p = ggplot(data=df1, aes(x=x_axis, y=y_axis, fill = bar_fill)) + geom_bar(stat = "identity")
p = p +  facet_grid(~ fgrid, scales = 'free')  
return(p)
}

And then run:

MyGridPlot(df1, df1Year, df1$Percentage, df1$Gender, df1$Group)

It comes up with the error:

Error: At least one layer must contain all faceting variables: `fgrid`.
* Plot is missing `fgrid`
* Layer 1 is missing `fgrid

I have tried using aes_string, which works for the x, y and fill but not for the grid.

MyGridPlot <- function (df, x_axis, y_axis, bar_fill, fgrid){
p = ggplot(data=df1, aes_string(x=x_axis, y=y_axis, fill = bar_fill)) + geom_bar(stat = "identity")
p = p +  facet_grid(~ fgrid, scales = 'free')  
return(p)
}

and then run:

MyGridPlot(df1, Year, Percentage, Gender, Group)

This produces the same error. If I delete the facet grid, both function code runs well, though no grid :-(

Thanks a lot for helping this beginner.

Gustavo

Upvotes: 5

Views: 1588

Answers (1)

benc
benc

Reputation: 376

Your problem is that in your function, ggplot is looking for variable names (x_axis, y_axis, etc), but you're giving it objects (df1$year...).

There are a couple ways you could deal with this. Maybe the simplest would be to rewrite the function so that it expects objects. For example:

MyGridPlot <- function(x_axis, y_axis, bar_fill, fgrid){ # Note no df parameter here
  df1 <- data.frame(x_axis = x_axis, y_axis = y_axis, bar_fill = bar_fill, fgrid = fgrid) # Create a data frame from inputs
  p = ggplot(data=df1, aes(x=x_axis, y=y_axis, fill = bar_fill)) + geom_bar(stat = "identity")
  p = p +  facet_grid(~ fgrid, scales = 'free')  
  return(p)
}

MyGridPlot(Year, Percentage, Gender, Group)

Alternatively, you could set up the function with a data frame and variable names. There isn't really much reason to do this if you're working with individual objects the way you are here, but if you're working with a data frame, it might make your life easier:

MyGridPlot <- function(df, x_var, y_var, fill_var, grid_var){
  # Need to "tell" R to treat parameters as variable names.
  df <- df %>% mutate(x_var = UQ(enquo(x_var)), y_var = UQ(enquo(y_var)), fill_var = UQ(enquo(fill_var)), grid_var = UQ(enquo(grid_var)))

  p = ggplot(data = df, aes(x = x_var, y = y_var, fill = fill_var)) + geom_bar(stat = "identity")
  p = p +  facet_grid(~grid_var, scales = 'free')  
  return(p)
}

MyGridPlot(df1, Year, Percentage, Gender, Group)

Upvotes: 2

Related Questions