. Advertisement .
..3..
. Advertisement .
..4..
When making plots with the ggplot2 package, you are going to use the scale_y_continuous
function in R regularly. Don’t worry if you aren’t familiar with it as this guide will show you how to make use of this function to modify your plots.
The scale_y_continuous Function In R
Position Scales In R And scale_y_continuous()/scale_x_continuous()
In R, we can use position scales to control how visual entities appear in the plot, including how their locations are mapped to the values of the database. With each plot, we have two position sales for the x and y aesthetics.
Most of the time, they can be found in the plot’s specification, and the user sets the values mapped to x and y. But this isn’t always the case. When the y or x aesthetics aren’t specified by the specification, you can modify it later.
For instance, the functions scale_y_continuous()
and scale_x_continuous()
can make changes to the y and x aesthetics in ggplot graphics. In simple cases, these functions map locations of position scales to data values.
Their syntaxes are similar:
scale_y_continuous(name, breaks, minor_breaks, n.breaks, labels, limits, expand, oob, na.value, trans, guide, position, sec.axis)
In which:
- name: the scale’s name, used as the legend title or axis. The default value is waiver(), which takes the name of the scale from that aesthetic’s first mapping. ggplot will omit the legend title if you set this argument to NULL>
- breaks: this argument sets the axis breaks for the plot. The default value is waiver(), which automatically computes breaks from the transformation object. A different function can be passed if it can return breaks from the limits. You can also set it to NULL if you want to have no breaks.
- labels: this argument determines the ggplot labels. The default value is waver(), which automatically gets labels from the transformation object. You can use a vector containing the labels (it must have the same length as breaks). Another function can also be used if it can produce labels from the breaks. If you want no labels, set this argument to NULL.
- limits: this argument sets the limits of the scale. To keep the default range, set it to NULL. You can use a vector of two numbers to indicate the scale limit as well. A function can be used if it can produce new limits from the existing ones.
Examples
Remember to install and load the ggplot2 package if you haven’t:
install.packages('ggplot2')
library('ggplot2')
Note: read this guide if you have trouble installing R packages.
We are going to use the built-in database mpg of the ggplot package. It contains some fuel economy data of car models released in the US between 1999 and 2008. First, we need to import the data and plot it:
p1 <- ggplot(mpg, aes(cyl, cty)) +
geom_point()
p1
......... ADVERTISEMENT .........
..8..
The x axis is the number of cylinders, while the y axis is the city miles per gallon of the same model. By default, ggplot uses the default labels: cyl and cty. We can change it to something more readable:
p1 +
scale_x_continuous("Number of cylinders") +
scale_y_continuous("City miles per gallon")
......... ADVERTISEMENT .........
..8..
This command will set the limits of the y scale to 15 and 30 miles. Keep in mind that ggplot will remove the data values out of this limit range. Here we use a numeric vector to specify these limits of the scale:
p1 + scale_y_continuous(limits = c(15, 30))
......... ADVERTISEMENT .........
..8..
With the breaks options, we can also control where the ticks should appear on the y axis:
p1 + scale_y_continuous(breaks = c(15, 17.5, 20, 22.5, 25, 27.5))
......... ADVERTISEMENT .........
..8..
Conclusion
The scale_y_continuous
Function In R allows us to modify the continuous position scales. There are a lot of things you can do with it, but what you should pay attention to is how to change the limits, labels, names, and breaks of the scale.
Leave a comment