0

I have the following function which runs correctly:

plot_agg <- function(df, group_type, x_label, y_axis) {

     ggplot(df, aes_string(x = colnames(df)[1], y = colnames(df)[4], group = colnames(df)[3], fill=colnames(df)[3])) + 
                    geom_bar(stat = "identity", position = "dodge") 
 }

}

When I enter an if statement, however, as follows:

plot_agg <- function(df, group_type, x_label, y_axis) {

     if (y_axis = "wins") {
            ggplot(df, aes_string(x = colnames(df)[1], 
                                  y = colnames(df)[4], 
                                  group = colnames(df)[3], 
                                  fill=colnames(df)[3])) + 
                    geom_bar(stat = "identity", position = "dodge") 
     }
}

it does not even load and gives me the following error:

Error: ggplot2 doesn't know how to deal with data of class function.

Any thoughts on why an if statement would cause this?

Alex Brown
  • 41,819
  • 10
  • 94
  • 108
asiehh
  • 553
  • 12
  • 22
  • 1
    It's your `if` statement. You need `y_axis == "wins"` not `y_axis = "wins"` – aosmith Oct 23 '15 at 20:10
  • 1
    Are you sure this is your exact function? What exactly are you passing to the function? It sounds like you're passing in `df` which is a base function in R and not necessarily like you've created a data.frame with the name `df`. A fully [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would be helpful here. – MrFlick Oct 23 '15 at 20:13
  • Thank you @aosmith. You are right. It was a careless mistake in the if statement. I guess i'm getting tired on Friday and missed an obvious thing. – asiehh Oct 23 '15 at 20:20
  • @MrFlick, yes. It's my exact function. I take it that using df is not a good variable name. – asiehh Oct 23 '15 at 20:24
  • The `=` vs `==` wouldn't explain the particular error message you got. There's still something going on outside of what you've shown us. And yes, i would recommend avoiding `df` as a variable name since it's already defined in base R. – MrFlick Oct 23 '15 at 20:26

0 Answers0