[R] Graphing a subset of data

Alexandra Hua alexandra.hua at yale.edu
Tue Dec 1 03:44:45 CET 2015

I am trying to write a function that will graph a variable of a dataset or
a variable from a subset of the data. My function is as follows:

graphics<-function(dat, var, graph, varname, val, subset){
  if(subset==1) {
  }else if(subset!=1){
  if(graph==1) {
    with(data, boxplot(var), main = paste("Vertical box plot of",
myfunc(dat), "variable", myfunc(var), xlab = myfunc(var)))
  }else if(graph==2){
    with(data, boxplot(var), horizontal=TRUE, main = paste("Horizontal
box plot of", myfunc(dat), "variable", myfunc(var), xlab =
  }else if(graph==3){
    hist(var, main="Histogram of", myfunc(var))

I included "subset" as a parameter for the function, so that subset=1 would
tell the function to subset, and any other value will use the full dataset.
However, when I run the function with the following expression (bolts is
the dataset, SPEED1 is the variable, value=3)

graphics(bolts, bolts$SPEED1, graph=3, bolts$SPEED1, 3, 1)

I receive this error message: Error in eval(substitute(expr), data, enclos
= parent.frame()) : invalid 'envir' argument of type 'logical'

Does anyone know why this is, or if there is something wrong with my code?

*Alexandra Hua *
Yale University | MPH Candidate Class of 2016
Chronic Disease Epidemiology

	[[alternative HTML version deleted]]

More information about the R-help mailing list