I am trying to plot a histogram inside a function like this:
norm.plot <- function(col_name, bw) {
temp_df = pitchbook[!is.na(pitchbook[col_name]), c(col_name)]
ggplot(temp_df, aes_string(x=col_name)) +
geom_histogram(color="blue", fill="cyan", binwidth = bw) +
stat_function(fun = function(x)
dnorm(x, mean = mean(temp_df[col_name]), sd = sd(temp_df[col_name])) * nrow(temp_df) * bw)
}
But I get an error
argument is not numeric or logical: returning NA
Computation failed in `stat_function()`: 'list' object cannot be coerced to type 'double'
How do I properly input the column name to avoid this error?