# How to set Ylim and Xlim in R (for graphs, with examples)

When you are doing graphs using R programming sometimes you want to define the scale of the axis. The plot function gives you the ability to define both the limit of the X and Y axes and simply define the limit of your charts. This process has the advantage of producing a better and more professional-looking data graph.

## Description

Setting the ylim and xlim parameters when making a plot is a simple matter of including these parameters when using the plot function. They are not required arguments, but they are handy. The most basic format for the plot function is plot(x,y) but you can use other parameters to adjust the labeling and scale of your graph. To adjust these two scale parameters, you equate each of them to a vector that defines the range of each of the axes. To do this the format is simply plot(x,y, xlim = vector, ylim = vector) where each vector represents the range of values being used. It is a simple process that allows you to set these parameters to any range that you want, even if your data falls outside of that range. Doing so will simply produce an empty plot regardless of your data content.

## Explanation

When you are doing a plot, in order for it to have meaning beyond being just a bunch of dots you need numerically labeled axes to tell you what the dots mean. Giving the axes names also helps provide meaning to what the graph is referring to. However, providing a scale on each axis makes it possible to know the values of the individual dots on your plot. This allows you to put the graph in perspective as to where each item is. Being able to set the ranges of the axes can help provide different perspectives on the data. Even producing a blank plot by shifting the range beyond that of the data can say something about that data. The purpose of data plots is to present a visual representation of data, the benefit of being able to adjust the scale of the axes can add additional information to that visualization.

## Examples

Here are three segments of r code that show the axis adjustment procedure and action. The first one shows the default, and the other two show the process under different circumstances.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = as.integer(abs(rnorm(50)*10))
> y = as.integer(abs(rnorm(50)*10))
> plot(x,y)

This is the default format of the plot function, note that it scales the axes by default based on the data.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = as.integer(abs(rnorm(50)*10))
> y = as.integer(abs(rnorm(50)*10))
> plot(x,y, xlim = c(0, 40), ylim = c(0, 40))

In this example, the xlim and ylim arguments are set to the same vector, with a range of zero to forty.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = as.integer(abs(rnorm(50)*20))
> y = as.integer(abs(rnorm(50)*10))
> plot(x,y, xlim = c(0, 50), ylim = c(0, 30))

In this example, the xlim and ylim arguments are set to different vectors, with ranges of zero to fifty and zero to thirty respectively. This illustrates each of the axes being given different values.

## Application

There are a number of applications to set the xlim and ylim arguments to specific scales. One is that you will know in advance what the range of your graph is. You can use it to show that the data is much smaller than a certain criterion. For example, you could use it to compare data points on the speed of various types of automobiles to that of a Formula One racer, without including the racing car among your data points. You can use this process to print out a certain range of your data when only part of it is relevant to the point you are trying to make. You can also use it to create an empty plot that shows that the actual data is out of range. It comes in handy under any circumstances where you want a specific range on your data plot.

Being able to adjust the range of each axis can come in handy under many circumstances. It gives you a considerable amount of control over any graph that you are plotting. This helps you to produce better quality work.

Scroll to top