I have a large dataset with multiple dependent variables but with only two independent variables (that I will be using over and over again to sort the many dependent variables). Each of the dependent variables was measured twice, once before and once after treatment. I would like to write a function that allows me to obtain a graph for each of these multiple dependent variables, with the arguments of the function as the two column names for whichever of the dependent variables i wish to graph.
I have generated a toy dataset to illustrate my problem. 't1DV1' and 't1DV2' are the pre- and post- treatment scores for dependent variable 1. 't1DV2' and 't2DV2' are pre- and post- treatment scores for dependent variable 2. 'group' is the independent variable.
group <- factor(rep(c("A", "B"), 10))
t1DV1 <- runif(20, min = 0, max = 10)
t2DV1 <- runif(20, min = 0, max = 10)
t1DV2 <- runif(20, min = 0, max = 10)
t2DV2 <- runif(20, min = 0, max = 10)
df <- data.frame(group, t1DV1, t2DV1, t1DV2, t2DV2)
df
I tried writing the following function
DVGraph <- function (DV1, DV2) {
require(tidyr)
dfLong <- gather(df, prePost, Score, DV1:DV1)
require(ggplot2)
barGraph <- ggplot(dfLong, aes(group, Score, fill = prePost)) +
geom_bar(stat = "identity", position = "dodge", size = 0.5) +
scale_fill_manual(values = c("#999999", "#666666")) +
xlab("") +
ylab("Scores") +
theme_bw()
return(barGraph)
}
And then tried calling it using the first of the repeated measures variables (I could equally have used the second, i.e. t1DV2 and t2DV2)
DVGraph(t1DV1, t2DV1)
But I get an error.
I tried using inverted commas like so
DVGraph("t1DV1", "t2DV1")
But i got another (different) error.
Does anyone know how I might go about this?
