Set Axis Limits in ggplot2 R Plot (3 Examples)
This tutorial explains how to set the axis limits of a ggplot in the R programming language.
The article consists of the following contents:
- Creation of Example Data & Basic Plot
- Example 1: Modification of Axis Limits with scale_x_continuous
- Example 2: Modification of Axis Limits with coord_cartesian
- Example 3: Cutting off Values with Zoom In
- Video & Further Resources
Let’s get started!
Creation of Example Data & Basic Plot
The following examples are based on this example data:
set.seed(10101) # Set seed data <- data.frame(x = rnorm(1000)) # Create example data
To create plots with the ggplot2 package, we need to install and load the package to RStudio:
install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package
Without any specifications of our axes, a basic density plot created with the ggplot2 package would look as follows:
ggplot(data, aes(x = x)) + # Create basic density ggplot geom_density()
Figure 1: Basic Density Plot of ggplot2 R Package.
As you can see based on Figure 1, ggplot2 automatically adjusts the axes so that all data points are shown. Furthermore the ggplot2 package leaves some space around the plotted data.
In the following examples, I’ll show you how to modify the axes of such ggplots. Note that this example is based on a density plot. However, the following R codes could be applied to any other types of plots and graphics (e.g. histogram, barchart, scatterplot, and so on…).
Example 1: Modification of Axis Limits with scale_x_continuous
We have basically two alternatives, if we want to change our ggplot2 axis ranges. The first alternative is based on the scale_x_continuous function:
ggplot(data, aes(x = x)) + # Density plot with scale_x_continuous geom_density() + scale_x_continuous(limits = c(-10, 10))
Figure 2: ggplot2 Density Plot with Broader x-Axis due to scale_x_continuous Function.
As you can see based on the previous R syntax, we specified the axis limits within the scale_x_continuous command to be within the range of -10 and 10. Of cause you could use any range you want.
Note: Equivalently to scale_x_continuous there also exists the scale_y_continuous function, which can be applied in the same manner in order to move the scale of the y-axis of a ggplot.
Let’s move on to the second alternative and its differences compared to Example 1…
Example 2: Modification of Axis Limits with coord_cartesian
Another way to widen the axis ranges of our plot relies on the coord_cartesian function:
ggplot(data, aes(x = x)) + # Density plot with coord_cartesian geom_density() + coord_cartesian(xlim = c(-10, 10))
Figure 3: ggplot2 Density Plot with Broader x-Axis due to coord_cartesian Function.
If you compare Figure 2 and Figure 3, you can see the difference between the two alternatives. The scale_x_continuous draws a black line at the bottom of our graph; but the coord_cartesian function does not.
Note: Within coord_cartesian you could also specify the ylim argument in order to adjust the y-axis.
Example 3: Cutting off Values with Zoom In
In the previous examples, I explained how to extent the x-axis range of a ggplot. However, we can also make the shown range smaller than the default:
ggplot(data, aes(x = x)) + # Zoom in geom_density() + scale_x_continuous(limits = c(-1, 1)) # Warning message: # Removed 328 rows containing non-finite values (stat_density).
Figure 4: Zoomed In Density Plot is Cutting of Some Values.
As you can see based on Figure 4, we zoomed in and cut off several values of our data. This is also the reason why the RStudio console returned the following warning message:
Removed 328 rows containing non-finite values (stat_density)
328 observations of our example are not shown in the zoomed in version of our plot.
Video & Further Resources
In case you need further explanations for the examples shown in this tutorial, you could have a look at the following video of the Statistics Globe YouTube channel. In the video, I’m explaining the examples of this article in more detail:
In addition, you may want to have a look at the other R tutorials of my website. You can find some interesting tutorials for handling of the ggplot2 package below. These tutorials are definitely a good basement for the data visualization with ggplot2:
- Adjust Space Between ggplot2 Axis Labels and Plot Area
- Rotate ggplot2 Axis Labels in R
- Remove Axis Labels & Ticks of ggplot2 Plot
- Change ggplot2 Legend Title
- Remove ggplot2 Legend Entirely
- Change Position of ggplot Title
- R Graphics Gallery
- The R Programming Language
In this article you learned how to set the axis limits of a ggplot in the R programming language. If you have comments or questions, don’t hesitate to let me know in the comments section below.