How to use yline to add horizonal lines to a plot in R

Sometimes when graphing data, it is necessary to include one or more horizontal lines to show where certain values are. R offers a couple of ways of doing this, but the fields package offers a simple function for this task and a lot more. It makes the process a lot easier than using base functions.


The function provided by the fields package for drawing a horizontal line is the yline function and it has the format of yline(y, lwd, col, lty) where “y” is the location on the y axis that the line is to be drawn, “lwd” is the width of the line to be drawn, “col” is the color of the line, and “lty” determines the spacing in the line if any. It has a companion function called the xline function that works exactly the same except that it produces vertical lines. These functions will draw a line across their designated axis at whatever value you specify, but they cannot initiate a plot and will produce an error message if called alone. You must first call the plot function or a similar function including one of several functions provided by the fields package. Combined these two functions allow you to draw both horizontal and vertical lines across a graph wherever you want. They are easy to use and understand making them an excellent addition to your R toolbox.


This function is one of many abline plotting options that can be added to a plot region after it has been set up. This requires first using the plot or similar function to create a plot region. When used it draws a line of a specified thickness at a specific location on the y axis. Because you can set the Y coordinate, color and even dash spacing You can create a wide variety of lines for different purposes using this function. It needs to be noted that you cannot use this function in isolation, but it has to be used with an active plot region. This function is part of the fields package which offers several other functions along with a number of useful datasets. When combined with other functions and data from this package it gives you the ability to produce some useful map plots. However, this function is useful even when using it without other content from within the package. You can use it to draw gridlines when you make a graph using R’s base plot function.


Here we have four examples of this function in action. Each one illustrates various aspects of the use of the function.

> library(fields)
> plot(5,5)
> yline(5)

This example illustrates the most basic use of this function. In this case, we simply tell it where to draw the line.

> library(fields)
> plot(5,5)
> yline( 5, lwd=7, col=2)

In this example, we not only define the line’s location but its width and color as well. In this case, the line is red and it is seven pixels thick.

> library(fields)
> plot(5,5)
> yline( 5, lwd=5, col=2, lty=3)

In this example, we not only define the line’s location, width, and color but we also add spacing to make it a dashed line.

> library(fields)
> plot(5,5)
> yline(seq(3,7, 1),col=3)
> xline(seq(3,7, 1),col=3)

In this example, we combine the yline and xline functions to produce a grid in our plot.


The yline function has many applications. The most common is creating a line in a data plot showing a specific value on the y axis. When used along with the xline function it can be used to create a full grid line system for a data plot. This makes the data a plot easier to understand than when it is just in the middle of a white space. Because the fields Package has several functions that are capable of drawing maps, you can use these two functions together with those functions to produce maps with latitude and longitude grids. This function can find applications in any place you need a line across your y-axis. This goes beyond maps but also includes any data plot you can make. These factors make it an incredibly useful function, particularly when working alongside the xline function. Once you get used to using it you are likely to find even more applications for this function.

This is an incredibly useful function and one that is quite simple to use and understand. The best way to learn how to use this function is to play around with it by changing the various values in the arguments to see what those changes do. This way, you also have fun in the process.

Scroll to top