When using the dataframe column inside the if else loop in stat_bin, throws object not found even though the dataframe and column exist. Here is a reproducible code
ggplot(mpg, aes(x = displ, fill = trans, label = trans)) +
geom_histogram(binwidth = 1,col="black") +
stat_bin(binwidth=1, geom="text", position=position_stack(vjust=0.5), aes(label=ifelse(..count..>4, as.character(trans), "")))
The above code throws following error
*Error in ifelse(count > 4, as.character(trans), "") : object 'trans' not found*
I even tried following with no luck
ggplot(mpg, aes(x = displ, fill = trans, label = trans)) +
geom_histogram(binwidth = 1,col="black") +
stat_bin(binwidth=1, geom="text", position=position_stack(vjust=0.5), aes(label=ifelse(..count..>4, mpg$trans, "")))
I get following error
*Error in ifelse(count > 4, mpg$trans, "") : object 'mpg' not found*
When I take out the if else and try following, it works fine(its detecting the dataframe and the column name)
ggplot(mpg, aes(x = displ, fill = trans, label = trans)) +
geom_histogram(binwidth = 1,col="black") +
stat_bin(binwidth=1, geom="text", position=position_stack(vjust=0.5), aes(label=as.character(trans)))
What I am missing here?