Now you know everything necessary to plot a 100% stacked bar chart, so let's use R to get it done. We will use code similar to what we used for the classic bar chart, but this time we will add a new argument to
The standard code for a bar chart in
ggplot(data = dataset, aes(x = variable_x, y = variable_y)) + geom_col()
variable_x as x, which means we will have one bar for each category of the x variable. We pass
variable_y as y, so segment height will be proportionate to the calculated y variable.
We want to divide our bar according to our groups, so we add the fill argument, like this:
ggplot(data = dataset, aes(x = variable_x, y = variable_y, fill = variable_group)) + geom_col()
variable_group as the
fill argument, so each category and segment will have its own color.