Unable to Pass Arguments in User Define Function t

2019-04-14 20:02发布

问题:

I want to create a user define function that wraps around some popular ggplot code I found. I am getting the following error:

"Error in `[.data.frame`(DS, , xvar) : object 'xcol' not found" 

The following is a small pseudo dataset to illustrate the issue.

n=25
dataTest <- data.frame(xcol=sample(1:3, n, replace=TRUE), ycol = rnorm(n, 5, 2), Cat=letters[1:5])

The user defined code is as here:

  TRIPLOT <- function (DS,xvar,yvar,zvar) {
 #localenv<-environment()
  gg <- data.frame(x=DS[,xvar],y=DS[,yvar],fill=DS[,zvar])
  empty<-ggplot()+geom_point(aes(1,1),colour="white") + theme( 
    plot.background = element_blank(),
    panel.background = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks = element_blank())
  scatter <-ggplot(gg, aes_string(x=xvar,y=yvar), environment=environment())+
    geom_point(aes_string(color=zvar))+
    scale_color_manual(values=c("orange","purple"))+
    theme(legend.position=c(1,1), legend.justification=c(1,1))
  plot_top <- ggplot(gg,aes_string(x=xvar, fill=zvar), environment=environment())+geom_density(alpha=.5)+scale_fill_manual(values=c("orange","purple"))+theme(legend.position="none")
  plot_right <- ggplot(gg, aes_string(yvar, fill=zvar),environment=environment())+geom_density(alpha=.5)+coord_flip()+ scale_fill_manual(values=c("orange","purple"))+
    theme(legend.position="none", axis.title.x=element_blank())
  PLT <- grid.arrange(plot_top,empty,scatter,plot_right, ncol=2,nrow=2,widths=c(4,1), heights=c(1,4))
  print(PLT)
}

The function I am trying to run is the following:

TRIPLOT(dataTest,"xcol","ycol","Cat")

I added environment argument and the data.frame() argument as recommend by the post. I have also passed the arguments to the function in quotes. Thank you for any help.

回答1:

Either pass the variable names as strings to the function, rename the variables in gg data frame; then use aes in place of aes_string; and use the gg data frame and its variable names in the calls to ggplot.

Or pass the variable names as strings to the function; then use aes_string (and skip the gg data frame).

In addition there is a problem with the number of colours in scale_fill_manual. I've commented those lines out.

library(gridExtra)
library(ggplot2)

n=25
dataTest = data.frame(
   xcol=sample(1:3, n, replace=TRUE), 
   ycol = rnorm(n, 5, 2), 
   Cat=letters[1:5])

EITHER:

 TRIPLOT <- function (DS,xvar,yvar,zvar) {
 #localenv<-environment()
  gg <- data.frame(x = DS[,xvar], y = DS[, yvar],fill = DS[,zvar])
  empty<-ggplot()+geom_point(aes(1,1),colour="white") + theme( 
    plot.background = element_blank(),
    panel.background = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks = element_blank())

  scatter <-ggplot(gg, aes(x = x,y = y))+
    geom_point(aes(color = fill))+
    #scale_color_manual(values=c("orange","purple"))+
    theme(legend.position=c(1,1), legend.justification=c(1,1))

  plot_top <-ggplot(gg,aes(x = x, fill = fill))+
   geom_density(alpha=.5)+
   #scale_fill_manual(values=c("orange","purple"))+
    theme(legend.position="none")

  plot_right <-ggplot(gg, aes(x = y, fill = fill))+
       geom_density(alpha=.5)+coord_flip()+ 
       #scale_fill_manual(values=c("orange","purple"))+
    theme(legend.position="none", axis.title.x=element_blank())

  PLT<-grid.arrange(plot_top,empty,scatter,plot_right, ncol=2,nrow=2,widths=c(4,1), heights=c(1,4))
  print(PLT)
}

TRIPLOT(dataTest,"xcol","ycol","Cat")

OR:

 TRIPLOT <- function (DS,xvar,yvar,zvar) {
 #localenv<-environment()
  #gg <- data.frame(x = DS[,xvar], y = DS[, yvar],fill = DS[,zvar])
  empty<-ggplot()+geom_point(aes(1,1),colour="white") + theme( 
    plot.background = element_blank(),
    panel.background = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks = element_blank())

  scatter <-ggplot(DS, aes_string(x = xvar,y = yvar))+
    geom_point(aes_string(color = zvar))+
    #scale_color_manual(values=c("orange","purple"))+
    theme(legend.position=c(1,1), legend.justification=c(1,1))

  plot_top <-ggplot(DS,aes_string(x = xvar, fill = zvar))+
   geom_density(alpha=.5)+
   #scale_fill_manual(values=c("orange","purple"))+
    theme(legend.position="none")

  plot_right <-ggplot(DS, aes_string(x = yvar, fill = zvar))+
       geom_density(alpha=.5)+coord_flip()+ 
       #scale_fill_manual(values=c("orange","purple"))+
    theme(legend.position="none", axis.title.x=element_blank())

  PLT<-grid.arrange(plot_top,empty,scatter,plot_right, ncol=2,nrow=2,widths=c(4,1), heights=c(1,4))
  print(PLT)
}

TRIPLOT(dataTest,"xcol","ycol","Cat")