Reputation: 23
I have a df that looks like:
> bank
Groups: Income_Category [5]
Income_Category `Attrited Customer` `Existing Customer`
<chr> <int> <int>
1 $120K + 126 601
2 $40K - $60K 271 1519
3 $60K - $80K 189 1213
4 $80K - $120K 242 1293
5 Less than $40K 612 29
I'm looking to create a side-by-side bar chart that's grouped by income level on the X-axis, simply showing counts on the y-axis. To further describe, it should be 5 groupings of 2 bars for numbers of attrited/existing customers.
I'm just really lost as to how I can set the y and fill args in ggplot. The following code does not work but this is how I understand ggplot to work:
ggplot(bank, aes(x = Income_Category, y = stat(count),fill = c(`Attrited Customer`, `Existing Customer`))) +
geom_col(position = "dodge")
I've searched for similar situations through google/stackOF, documentations, etc but nothing thus far has elucidated how to alter y = and fill = to generate a working plot. Any help or pointers in the right direction are greatly appreciated!!
Upvotes: 0
Views: 188
Reputation: 389235
It is easier to plot once you get the data in long format.
library(ggplot2)
long_data <- tidyr::pivot_longer(bank, cols = -Income_Category)
long_data$Income_Category <- factor(long_data$Income_Category,
c('Less than $40K', '$40K - $60K', '$60K - $80K', '$80K - $120K', '$120K +'))
Here are some options for plotting.
1.
ggplot(long_data) + aes(name, value, fill = Income_Category) + geom_col()
2.
ggplot(long_data) + aes(Income_Category, value, fill = name) + geom_col()
3.
ggplot(long_data) + aes(Income_Category, value, fill = name) + geom_col(position = 'dodge')
4.
ggplot(long_data) + aes(Income_Category, value) + geom_col() + facet_wrap(~name)
data
It is easier to help if you share data in a format which is easier to copy.
bank <- data.frame(Income_Category = c('$120K +', '$40K - $60K', '$60K - $80K', '$80K - $120K', 'Less than $40K'),
"Attrited Customer" = c(126, 271, 189, 242, 612),
"Existing Customer" = c(601, 1519, 1213, 1293, 29))
Upvotes: 1