1

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:

enter image description here

1053Inator
  • 302
  • 1
  • 15
  • `object "v1" not found`: that's what I got. Please edit to make it reproducible. – nicola Jun 08 '15 at 14:26
  • the problem is not really mapply, did you try to run your function for just one plot ? – Cath Jun 08 '15 at 14:44
  • The function works if environment = environment() is added ([known bug](http://stackoverflow.com/questions/5106782/use-of-ggplot-within-another-function-in-r)). But the problem with `mapply` persists. – 1053Inator Jun 08 '15 at 14:53
  • @1053Inator, I edited my answer with an option that works for me, ie working with variable names (because of mapply) and adding print to print the plot – Cath Jun 08 '15 at 15:03

1 Answers1

2

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:

baseplot <- function(var1, var2){
    plot(df[,var1], df[,var2], pch=19)
}
par(mfrow=c(1, 2))
mapply(baseplot, c("V1", "V3"), c("V2", "V4"))

enter image description here

EDIT
With ggplotand putting parameter environment=environment(), adding print and using variable names (between quotes) seems to work:

 ScatterPlot <- function(var1, var2) {
     print(ggplot(data = df,
     aes(x = df[,var1], y = df[,var2]),
     environment = environment()) +
     geom_point())
 }

mapply(ScatterPlot, var1=c("V1", "V3"), var2=c("V2", "V4"))
Cath
  • 23,906
  • 5
  • 52
  • 86
  • Thanks, this works fine. I still find it strange you have to use `df[ ,var]` instead of just `var` and I also fail to see why the quotes are necessary. Could you also explain how you got the 2 graphs in the same panel? – 1053Inator Jun 08 '15 at 15:14
  • @1053Inator, you can't have a "vector of variables" in the mapply call so you need to put a vector of variable names. To have the 2 plots, I used `par(mfrow=c(1, 2))`, telling R to divide the plot area into one row and 2 columns. It works for base R plots, not sure it also does with ggplot – Cath Jun 09 '15 at 06:45
  • With ggplot, I usually use grids. library("grid") and library("gridExtra") - then grid.arrange(plot1, plot2, plot3, plot4, ncol=2) gives you a nice four panel array of plots. If your plots are in a list, you have to tweak it more like do.call("grid.arrange", c(list.of.plots, ncol=2)). Hope that helps. – mightypile Oct 12 '17 at 15:37