HOWTO · R

ylim() and xlim() in R

This tutorial explores how to effectively use ylim() and xlim() in R for setting axis limits in data visualizations. Learn how to enhance your plots with clear examples and detailed explanations. Discover the importance of controlling axis limits to emphasize key data trends and improve your visual storytelling skills.

On this page

In base R graphics, set the vertical axis range with ylim and the horizontal range with xlim. Pass either argument, or both, to a plotting function such as plot() as a pair of lower and upper bounds. The example below shows both limits on one scatter plot, followed by guidance for adjusting each axis and for ggplot2.

What are ylim() and xlim()?

Setting axis limits in a base R plot means passing xlim for the horizontal axis, ylim for the vertical axis, or both to a plotting function such as plot(). Each argument takes a numeric vector with the lower and upper bounds. This complete example uses the same data to show both limits together:

x <- 1:10
y <- c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)

# Set both visible axis ranges in a base R scatter plot.
plot(x, y, xlim = c(2, 8), ylim = c(0, 30),
     xaxs = "i", yaxs = "i",
     main = "Selected ranges for x and y")

The xlim and ylim arguments supply the requested ranges. Base graphics normally adds a small expansion at each end of an axis; xaxs = "i" and yaxs = "i" turn off that padding, so this example displays the supplied bounds of 2 to 8 and 0 to 30. The points at x values 1, 9, and 10 fall outside the displayed horizontal range. The code uses base graphics, which is included with R; it does not require an additional package. The example runs successfully and produces no text on standard output or standard error.

In base R graphics, ylim and xlim are arguments that let you choose the plotted y-axis and x-axis ranges. The names are often written as ylim() and xlim() when discussing the options, but in a call to plot() you supply the bounds to the named arguments, as in ylim = c(0, 30). The first number is the lower bound and the second is the upper bound. R normally selects ranges from the supplied data; explicit limits are useful when you want a consistent scale across plots or want to focus attention on a particular interval.

These arguments are part of base graphics functions such as plot(). They are not universal parameters shared by every R plotting system. In particular, ggplot2 uses a different interface, described below.

Using ylim() to Set Y-Axis Limits

Use the ylim argument to set the vertical range. For example, plot(x, y, ylim = c(0, 30)) supplies 0 and 30 as the y-axis limits while leaving the x-axis range to be chosen from the x data. With the default yaxs = "r" style, base graphics adds padding to the displayed range; add yaxs = "i" when the axis should display exactly the supplied bounds. The bounds should use the same units as the plotted y values. A wider range can leave room for labels or make several plots comparable; a narrower range can make variation within a selected interval easier to see.

In base graphics, points or portions of graphical objects outside the chosen plotting region are not visible in that region. Choose limits with the data and the comparison you want readers to make in mind. A plot may still be drawn with the requested axis range even when some observations lie beyond it, so the visible panel should not be mistaken for the full data range.

Using xlim() to Set X-Axis Limits

The xlim argument controls the horizontal range. For instance, plot(x, y, xlim = c(2, 8)) supplies 2 and 8 as the x-axis limits, while plot() chooses the y-axis range from the y data. The default xaxs = "r" style adds padding to the displayed range; use xaxs = "i" to display exactly the supplied bounds. This is useful for focusing on a time interval, a subset of measured values, or a common horizontal scale across multiple plots.

As with ylim, supply the limits in the units of the x variable and in lower-to-upper order for the usual increasing axis. If observations are outside the visible interval, they do not appear in the plotting region. Check the selected interval against the original data so a focused view does not hide context your reader needs.

Combining ylim() and xlim() for Enhanced Control

Pass both arguments to the same base graphics call to supply a rectangular viewing range, as in the opening example. Set xlim from the horizontal values you want to show and ylim from the vertical values you want to show. By default, base graphics expands each range slightly; include xaxs = "i" and yaxs = "i" when you want the displayed axes to end exactly at the supplied limits. When comparing plots, using the same limits makes their scales easier to compare; when exploring one plot, choose bounds that preserve the context needed to interpret the pattern.

The behavior depends on the plotting system. In ggplot2, axis limits are commonly specified with scales such as scale_x_continuous(limits = c(2, 8)) and scale_y_continuous(limits = c(0, 30)). Scale limits censor out-of-range values before later plot layers or statistical summaries are drawn, which can affect those results. For a view-only zoom that retains the data for calculations, use coord_cartesian(xlim = c(2, 8), ylim = c(0, 30)). These ggplot2 options are not xlim and ylim arguments to plot(); use the interface for the plotting system in your code.

Conclusion

For a base R plot, set the horizontal range with xlim and the vertical range with ylim; pass both when you need to control both axes. Select bounds that support the comparison, and remember that observations outside the visible region may not appear. In ggplot2, use scale limits or coord_cartesian() according to whether you want limits to affect the plotted data or only the view.

FAQ

  1. Can I set both axis limits in one plot? Yes. Include xlim = c(lower_x, upper_x) and ylim = c(lower_y, upper_y) in the same base graphics call.

  2. How should I choose the bounds? Use the units and range of each variable, the comparisons readers need to make, and whether multiple plots must share a scale. Check that the chosen view leaves enough context to interpret the data.

  3. What happens to points outside the limits? In base graphics, they are outside the visible plotting region and are not shown there. In ggplot2, scale limits can remove out-of-range values from later calculations; coord_cartesian() zooms the view while retaining the data for those calculations.

  4. Are xaxs and yaxs alternatives to xlim and ylim? They control axis expansion in base graphics, such as padding around the plotted range. Use xlim and ylim to supply axis limits; the default axis style may expand the displayed ranges. Set xaxs = "i" and yaxs = "i" when the displayed axes must end exactly at those limits.