Only this week, get the SQL Complete Track of 9 courses in a special prize of $330 $89!
Visualize your data 1
Visualize your data 2
7. Draw the 100% stacked bar chart
Work with your chart
Check yourself


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 aes.

The standard code for a bar chart in ggplot2 is:

ggplot(data = dataset, aes(x = variable_x, y = variable_y)) + geom_col()

We pass 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()

We pass variable_group as the fill argument, so each category and segment will have its own color.


Let's plot our data on the 100% stacked bar chart. Use the above syntax and make france_beverages the dataset. Set country as x, percent as y, and beverage as fill in ggplot(). Add geom_col(width = 0.3) to ggplot using the + sign.

Pass the results to the stacked variable. Press Run and Check Code when you're done.

Stuck? Here's a hint!

You should write:

stacked <- ggplot(
  data = france_beverages, 
  aes(x = country, y = percent, fill = beverage)) + 
    geom_col(width = 0.3)