new to ggplot2, I've scoured the web but still couldn't figure this out. I understand how to plot a boxplot in ggplot2, my problem is that I can't pass along the variable I use for groups when it is inside a function.
so, normally (i.e. NOT inside a function), I would write this:
ggplot(myData, aes(factor(Variable1), Variable2)) +
geom_boxplot(fill="grey", colour="black")+
labs(title = "Variable1 vs. Variable2" ) +
labs (x = "variable1", y = "Variable2")
Where myData is my data frame Variable 1 is a 2 level factor variable Variable 2 is a continuous variable I want to make boxplots of Variable 1 by its 2 levels/groups and this works fine, but as soon as I write this inside a function I couldn't get it to work.
my attempt in writing the function:
myfunction = function (data, Variable1) {
ggplot(data=myData, aes_string(factor("Variable1"), "Variable2"))+
geom_boxplot(fill="grey", colour="black")+
labs(title = paste("Variable1 vs. Variable2" )) +
labs (x = "variable1", y = "Variable2")
}
this only gives me a single boxplot(instead of 2), as if it never understood the factor(Variable1) command (and did a single boxplot of the entire Variable 2, rather than separate them by Variable 1 level first, then boxplot them).
