r - Fetch the original name of vector variable passed to a custom function -


my problem best understood after reading r script:

plothistogram <- function(data, x, binw=0.30, color=false, density=false, h=10, w=10) {      require(ggplot2)     # data$x <- as.numeric(x)      plot <- ggplot(data, aes(x))       if(density & color) {       plot <- plot + geom_density(fill="blue", aes(alpha=0.2))     } else if (density) {       plot <- plot + geom_density()     } else if (color) {       plot <- plot +          geom_histogram(binwidth=binw, aes(y=..density.., fill=..count..)) +         scale_fill_gradient("count", low="purple", high="red")     } else {       plot <- plot + geom_histogram(binwidth=binw, aes(y=..density.., fill=..count..))     }      ggsave(file="histo_plot.svg", plot=plot, height=h, width=w)      return(plot) } 

notice commented line. have use ggplot recognize x belongs data. hack. intent plot name of whatever series input x. i'm not sure how because far can tell, have store column visualized in variable, variable renames vector, there's no way find original name in data.frame.

i'm sure i'm overlooking simple solution.

edit: concretely expressed problem

suppose did following:

plothistogram(economics, economics$psavert) 

this result in either error, or undesired result. if uncomment line, x-axis label "x" drawn, when want "psavert".


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -