Get startedGet started for free

Side-by-side bar charts

While a contingency table represents the counts numerically, it's often more useful to represent them graphically.

Here you'll construct two side-by-side bar charts of the comics data. This shows that there can often be two or more options for presenting the same data. Passing the argument position = "dodge" to geom_bar() says that you want a side-by-side (i.e. not stacked) bar chart.

This exercise is part of the course

Exploratory Data Analysis in R

View Course

Exercise instructions

  • Load the ggplot2 package.
  • Create a side-by-side bar chart with align on the x-axis and gender as the fill aesthetic.
  • Create another side-by-side bar chart with gender on the x-axis and align as the fill aesthetic. Rotate the axis labels 90 degrees to help readability.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Load ggplot2


# Create side-by-side bar chart of gender by alignment
ggplot(___, aes(x = ___, fill = ___)) + 
  geom_bar(position = ___)

# Create side-by-side bar chart of alignment by gender
ggplot(___, aes(x = ___, fill = ___)) + 
  geom_bar(___) +
  theme(axis.text.x = element_text(angle = ___))
Edit and Run Code