joey711/phyloseq — Issue #83: deparse(substitute(x)) issue in plot_richness_estimates()
Author: joey711
Created: 2012-03-05 20:28:17+00:00
State: open
Labels: Bug

Potential bug, the following code:

```R
# If manually-supplied x or color, add to DF, dummy var_name
if(length(color) > 1){
DF <- data.frame(DF, manual_color = color)
color <- deparse(substitute(color))
}
```

Should be changed to

```R
if(length(color) > 1){
DF$color <- color
names(DF)[names(DF)=="color"] <- deparse(substitute(color))
color <- deparse(substitute(color))
}
```

Interestingly, the former works as expected in many examples, but I believe this is because the relevant variable still exists in the search path up to the global environment. This is dangerous if this function gets wrapped in other functions, or other types of situations where the search path is less simple. The latter approach ensures that the name of the vector provided for the color variable is also the name in the plotting `data.frame`, and consequently it is the first, and therefore only, place that `R` will look for the color data during the aesthetic mapping. Much safer.

NOTE: this same code is going to be repeated several times in related plotting functions. If possible, this is a good opportunity to make an internal function, or set of functions, that will handle this in one line.