I made the following dataframe df
:
V1 <- 1:10
V2 <- 11:20
V3 <- 21:30
V4 <- 31:40
df <- data.frame(V1,V2,V3,V4)
I also made a function which should make a simple scatterplot based on the arguments var1
and var2
.
ScatterPlot <- function(var1, var2) {
ggplot(data = df,
aes(x = var1, y = var2)),
environment = environment() +
geom_point()
}
I only want 2 specific scatterplots for the following combinations of variables: v1-v2
and v3-v4
.
I thought mapply
would come in handy here, looping over different combinations of variables.
mapply(FUN = ScatterPlot,
var1 = c(V1, V3),
var2 = c(V2, V4))
I just expected 2 plots but this is what I got instead:
I think the problem came from the fact that the function is looking for a variable inside df named "var1". I don't know ggplots enough to circumvent this problem but, with base R plot, you can do:
EDIT
With
ggplot
and putting parameterenvironment=environment()
, addingprint
and using variable names (between quotes) seems to work: