I have been trying to figure this out for a while now and I have also searched a lot on the internet, but I could not find a solution to this problem.
I have the following code which works, but I can't get the column names to appear as titles in the plot:
lapply(bank, function(a) {
if (is.factor(a)){
ggplot(bank, aes(x=a)) + geom_bar(aes(fill = bank$y), position = "fill") + geom_hline(yintercept = 0.1127) + geom_bar(aes(y = (..count..)/sum(..count..), alpha = 0.3), show.legend = FALSE)
}
else {
ggplot(bank, aes(x = a)) + geom_histogram(aes(fill = bank$y), position = "fill") + geom_hline(yintercept = 0.1127) + geom_density(aes(y = ..scaled..))
}
})
I tried putting "a" in ggtitle and a bunch of other stuff but nothing worked. I would be greatful for any help!
Edit: I tried using this code now (looping through the names and using aes_string):
lapply(names(bank), function(a) {
if (is.factor(bank[[a]])){
ggplot(bank, aes_string(x = a)) + geom_bar(aes(fill = bank$y), position = "fill") + geom_hline(yintercept = 0.1127) + geom_bar(aes(y = (..count..)/sum(..count..), alpha = 0.3), show.legend = FALSE)
}
else {
ggplot(bank, aes_string(x = a)) + geom_histogram(aes(fill = bank$y), position = "fill") + geom_hline(yintercept = 0.1127) + geom_density(aes(y = ..scaled..))
}
})
this code works, the error message was because of whitespace in the variable name