I find it hard to formulate a question that fits, so I illustrate my problem in an example. I'd like to create similar plots for multiple variables in my dataset with a specific function. The function creates a summary data frame and produces a plot.
So without a function I would do the following, using the summarySE() function (source)
library(ggplot2)
library(plyr)
#data
df <- data.frame(case=rep(1:5,3),
value=rnorm(15))
#summarise
df_sum <- summarySE(df, "value", groupvars=c("case"))
#plot
ggplot(df_sum, aes(x=case, y=value)) +
geom_point()
Now I tried to write a function that takes a data frame and a variable in that data frame and produces the plot above.
#function reproducing a nonsensical plot
f <- function(data,var){
data_sum <- summarySE(data, var, groupvars=c("case"))
x <- ggplot(data_sum, aes(x=case, y=var)) +
geom_point()
return(x)
}
f(df,"value")
I thought I could benefit from the fact, that the summarised variable in df_sum is named the same as the variable in df. But because ggplot() takes the string that I called in f(), it produces a nonsensical plot.
Does someone have a general solution how to adress problems like that? It is not the first time I ran into this type of problem with strings used as input in a function. I tried unquote() and similar things but nothing worked.