Learn

Instead of stacking our fill variable in our bar graphs, we can also represent values of the variable side by side. This is known as a clustered bar plot.

The plot below visualizes our msleep data for elephants as a clustered bar plot instead of a stacked bar plot. Notice how the values shown are the same as in our stacked bar plot, but the positioning is now different.

Clustered Bar Plot: Hours of Day by Sleep State

We can easily create a clustered bar plot by setting position = "dodge" instead of position = "stack" in our geom. The dodge positioning tells ggplot2 to display each value of the fill variable next to each other (as opposed to on top of each other in "stack"). Once again, we can either use geom_bar(stat = "identity") or geom_col().

# Filter our data to include only elephants msleep_clustered_df <- msleep %>% filter(order == "Proboscidea") # Construct a clustered bar plot msleep_stackedbar <- ggplot(msleep_clustered_df, aes(x = name, y = hours, fill = status)) + labs(title = "Hours of Day by Sleep State") + geom_bar(position = "dodge", stat = "identity") # This creates the same plot! msleep_stackedbar <- ggplot(msleep_clustered_df, aes(x = name, y = hours, fill = status)) + labs(title = "Hours of Day by Sleep State") + geom_col(position = "dodge")

Instructions

1.

Continuing with our graduation_df data, we now want to create a clustered bar plot visualizing the percent of students each year who graduated at one school, for students who are English proficient and English language learners.

We’ve created a new data frame called graduation_clustered_df with these records. Examine graduation_clustered_df using the head() function.

2.

Create a clustered bar plot named graduation_clusteredbar that maps Year to the x axis, Pct to the y axis, and Demographic to the fill variable. Use geom_col(), making sure to specify the correct position value.

Print the clustered bar plot we just created to see what it looks like.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?