[R] Turning a variable name into a function argument

KMNanus kmnanus at gmail.com
Tue Jun 28 03:12:20 CEST 2016


I’m inexperience but am trying to get my head around using functions to make a number of ggplots easier to do.  

I have a function that creates a ggplot taking one input variable as an argument. The variable name is shorthand for the actual variable (variable name = tue, Actual name = Tuesday).  Since I want to use the actual variable name in ylab and ggtitle, I’d like to add a second argument, new.name, to the function which would allow me to utilize both inputs as arguments but have not been successful.  I tried creating a function within the function to accomplish this, using deparse(substitute(new.name))and also using the code you see below.


myfun <- function(myvar, new.name){
  function(new.name){return(as.character(substitute(new.name)))}
  ggplot(b12.2, aes(x= games,  y = myvar, col = Group))+
  geom_point() + 
  geom_line()+
  xlab("Minimum Games" ) +
  ylab(paste(new.name, “Average Change"))+
  ggtitle(new.name, "Change \n as a Function of Minimum Number of Games”)+
  theme_bw()

When call myfun(myvar, new.name), I get an error msg “new.name is not found” whether I call new.name or Tuesday.

I want ggplot to automatically insert Tuesday into ylab and ggtitle.

Can anyone help me with this?  Thanks for your patience.

Ken
kmnanus at gmail.com
914-450-0816 (tel)
347-730-4813 (fax)





More information about the R-help mailing list