Reputation: 1967
I have the following function that I'd like to use to generate density histograms of various features in a dataset:
fraudSplitHist <- function(dataframe, stat_col, signal_col='fraud') {
hist_data <- dataframe[,c(stat_col, signal_col)]
colnames(hist_data) <- c('stat', 'fraud')
hist_data$stat <- winsor1(hist_data$stat) # remove outliers
ggplot(hist_data, aes(hist_data$stat, fill=as.factor(hist_data$fraud))) +
geom_histogram(alpha=0.5, aes(y=..density..), position='identity', binwidth=1)
}
Running it always give me this error
fraudSplitHist(dataframe=data, stat_col='some_column_of_values')
Error in eval(expr, envir, enclos) : object 'hist_data' not found
However, it works with qplot
fraudSplitHist <- function(dataframe, stat_col, fraud_col='fraud') {
hist_data <- dataframe[,c(stat_col, fraud_col)]
colnames(hist_data) <- c('stat', 'fraud')
hist_data$stat <- winsor1(hist_data$stat)
qplot(data=hist_data, x=stat, geom="histogram", fill=as.factor(fraud), binwidth=10,
position='identity')
}
Any ideas what I could be missing here?
Upvotes: 2
Views: 704
Reputation: 206207
You should not specify the data.frame name in your aes()
settings. You should be just using column names from the data.frame. For example, this should work
simpletest <- function(dataframe, x_col, y_col='cyl') {
plot_data <- dataframe[,c(x_col, y_col)]
colnames(plot_data) <- c('stat', 'fraud')
ggplot(plot_data, aes(stat, fill=as.factor(fraud))) +
geom_histogram(alpha=0.5, aes(y=..density..), position='identity', binwidth=1)
}
simpletest(mtcars, "disp")
Upvotes: 3