Learn

Frequently, we’ll want to customize our axes to represent our data more clearly. For discrete variables, such as categories on the x axis of a bar plot, we may want to specify a particular order these values appear in. Or, we might want to rename the axes labels so they better describe each value.

We can customize discrete variables on the x axis with the scale_x_discrete() layer. There is also a scale_y_discrete() layer that works the same way for variables on the y axis.

Let’s take a look at the hours of sleep by diet plot we created in the last exercise. We can pass a vector to the limits argument in scale_x_discrete() to specify that we only want to show bars for c("omni", "carni", "herbi"), in that order rather than the default alphabetical ordering.

msleep_start <- ggplot(msleep_error_df, aes(x = diet, y = mean.hours)) + geom_bar(stat = "identity") + geom_errorbar(aes(ymin = se.min, ymax = se.max), width = 0.2) + labs(title = "Mean Hours Asleep by Diet") msleep_discrete <- msleep_start + scale_x_discrete( limits = c("omni", "carni", "herbi"))

This produces the following plot. "insecti" is now omitted on the x axis. "omni" appears on the left, whereas it would appear on the right in the default alphabetical ordering.

Discrete Axes Limits: Mean Hours Asleep by Diet

We might also want the labels to be more descriptive – for example, "carnivore" is a more familiar word compared to "carni". We can pass a vector of value-to-label mappings to the labels argument in scale_x_discrete() to specify what we want to show for each existing value.

msleep_discrete <- msleep_start + scale_x_discrete( limits = c("omni", "carni", "herbi"), labels = c("carni" = "Carnivore", "herbi" = "Herbivore", "omni" = "Omnivore"))

This produces the following plot. Now the labels are much more clear! The ordering we specify in limits is still preserved.

Discrete Axes Labels: Mean Hours Asleep by Diet

Instructions

1.

Let’s return to our plot of mean graduation rates by year. We’ve loaded our preprocessed data frame and included starting code to create this plot again. Add a scale_x_discrete() geom and use its limits argument to only show graduation rates for years "2003", "2004", and "2005".

Then, print graduation_discrete to see what it looks like.

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?