Learn

Similarly to discrete variables, we can add a scale_x_continuous() layer to customize continuous variables on our x axis, or a scale_y_continuous() layer to customize continuous variables on our y axis. We can also add a coord_cartesian layer to specify the range of values shown on a given axis, allowing us to zoom in and out of the plot region.

Continuing with our plot of sleep hours by diet, let’s set the y axis to be between 8 and 12. Most animals sleep at least few hours a day, so we don’t need our y axis to start exactly at 0. We can adjust our y axis ranges by adding a coord_cartesian() layer and passing a vector c(8, 12) to its ylim argument, specifying the min and max values of the y axis.

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) + scale_x_discrete( limits = c("omni", "carni", "herbi"), labels = c("carni" = "Carnivore", "herbi" = "Herbivore", "omni" = "Omnivore")) + labs(title = "Mean Hours Asleep by Diet") msleep_continuous <- msleep_start + coord_cartesian(ylim = c(8, 12))

Now our plot looks like this. Our y axis begins at 8 and ends at 12 as we specified. The differences between each bar’s height are much more obvious – omnivores sleep the most on average, while herbivores sleep the least!

Axes Ranges: Mean Hours Asleep by Diet

We can also customize the labels shown for the y axis tick marks using the breaks argument of the scale_y_continuous() layer. In the code below, we pass a vector c(8, 10, 12) to specify that we only want tick marks on the y axis to appear at 8, 10, and 12.

msleep_continuous <- msleep_start + coord_cartesian(ylim = c(8, 12)) + scale_y_continuous(breaks = c(8, 10, 12))

Here is our new plot:

Axes Ranges: Mean Hours Asleep by Diet

Finally, we can apply custom transformations to our tick mark labels. Let’s say we want to add a unit of measurement “hrs” to each number on the y axis. We can pass a custom function to the labels argument of scale_y_continuous(). Here, we are telling ggplot2 to take the automatic labels and add the characters " hrs" after each label.

show_as_hours <- function(x) { output <- paste0(x, " hrs") return(output) } msleep_continuous <- msleep_start + coord_cartesian(ylim = c(8, 12)) + scale_y_continuous(labels = show_as_hours, breaks = c(8, 10, 12))

Here’s what our plot with its newly labeled y axis looks like:

Axes Ranges: Mean Hours Asleep by Diet

Instructions

1.

Continuing with our graduation_df plot of mean graduation percentages by year, complete the starting code provided by adding a scale_y_continuous() layer to customize the y axis. Display the y axis as percentages (e.g. 60%) rather than ratios (e.g. 0.6) using a custom function specified in the labels argument. The scales package comes automatically loaded with ggplot2 and includes a variety of functions for transforming labels. Apply the function scales::label_percent() to our y axis labels.

Print graduation_continuous 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?