13

I need to wrap ggplot2 into another function, and want to be able to parse variables in the same manner that they are accepted, can someone steer me in the correct direction.

Lets say for example, we consider the below MWE.

#Load Required libraries.
library(ggplot2)

##My Wrapper Function.
mywrapper <- function(data,xcol,ycol,colorVar){
  writeLines("This is my wrapper")
  plot <- ggplot(data=data,aes(x=xcol,y=ycol,color=colorVar)) + geom_point()
  print(plot)
  return(plot)
}

Dummy Data:

##Demo Data
myData <- data.frame(x=0,y=0,c="Color Series")

Existing Usage which executes without hassle:

##Example of Original Function Usage, which executes as expected
plot <- ggplot(data=myData,aes(x=x,y=y,color=c)) + geom_point()
print(plot)

Objective usage syntax:

##Example of Intended Usage, which Throws Error ----- "object 'xcol' not found"
mywrapper(data=myData,xcol=x,ycol=y,colorVar=c)

The above gives an example of the 'original' usage by the ggplot2 package, and, how I would like to wrap it up in another function. The wrapper however, throws an error.

I am sure this applies to many other applications, and it has probably been answered a thousand times, however, I am not sure what this subject is 'called' within R.

plannapus
  • 18,529
  • 4
  • 72
  • 94
Nicholas Hamilton
  • 10,044
  • 6
  • 57
  • 88

2 Answers2

12

The problem here is that ggplot looks for a column named xcol in the data object. I would recommend to switch to using aes_string and passing the column names you want to map using a string, e.g.:

mywrapper(data = myData, xcol = "x", ycol = "y", colorVar = "c")

And modify your wrapper accordingly:

mywrapper <- function(data, xcol, ycol, colorVar) {
  writeLines("This is my wrapper")
  plot <- ggplot(data = data, aes_string(x = xcol, y = ycol, color = colorVar)) + geom_point()
  print(plot)
  return(plot)
}

Some remarks:

  1. Personal preference, I use a lot of spaces around e.g. x = 1, for me this greatly improves the readability. Without spaces the code looks like a big block.
  2. If you return the plot to outside the function, I would not print it inside the function, but just outside the function.
Tung
  • 26,371
  • 7
  • 91
  • 115
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Thanks, didn't know there was the aes_string option, but isn't there a way to pass on the 'name', I mean, how does say the ggplot2 function know what the arguments mean. – Nicholas Hamilton Apr 30 '13 at 09:02
  • There are ways of getting the name of object, ggplot2 uses that under the hood in `aes`, but for passing arguments like this, `aes_string` is much easier. – Paul Hiemstra Apr 30 '13 at 09:05
  • Hi, this is very useful answer. I'm using this solution to develop a wrapper for a histogram. However, in my code I've code for generating median line `geom_vline(aes(xintercept = mean(histogramVariable)), colour = 'red'`. How smartly pass the `histogramVariable` as `data$histogramVariable` into this? – Konrad Nov 11 '15 at 12:22
2

This is just an addition to the original answer, and I do know that this is quite an old post, but just as an addition:

The original answer provides the following code to execute the wrapper:

mywrapper(data = "myData", xcol = "x", ycol = "y", colorVar = "c")

Here, data is provided as a character string. To my knowledge this will not execute correctly. Only the variables within the aes_string are provided as character strings, while the data object is passed to the wrapper as an object.

Ben
  • 99
  • 6