How to Create a Horizontal Legend in Base R (2 Methods)

There are two ways to create a horizontal legend in Base R. The first method is to use the legend() function with the side argument set to “bottom”. The second method is to use the mtext() function with the side argument set to “bottom” and the line argument set to “outer”. Both of these methods will create a horizontal legend in Base R.


You can use the following methods to create a horizontal legend in a plot in base R:

Method 1: Use the horiz argument

legend('bottom', fill=fill_cols, legend=c('A', 'B', 'C', 'D', 'E', 'F'),
        horiz=TRUE, inset=c(0, -.1), xpd=TRUE)

This particular example creates a horizontal legend below the plot in which each item in the legend is on the same row.

Method 2: Use the ncol argument

legend('bottom', fill=fill_cols, legend=c('A', 'B', 'C', 'D', 'E', 'F'),
        ncol=3, inset=c(0, -.15), xpd=TRUE)

This particular example creates a horizontal legend below the plot with three columns.

The inset(x, y) argument controls the location of the legend. By using negative values for the y-value, we can push the legend down outside of the plot.

The xpd=TRUE argument allows us to place the legend outside of the plot and still be seen in the plotting area.

The following examples show how to use each method in practice.

Example 1: Use horiz Argument to Create a Horizontal Legend in Base R

The following code shows how to use the horiz=TRUE argument within the legend() function to create a horizontal legend on the bottom of a plot in base R:

#create vector of values
data <- c(4, 10, 7, 5, 4, 3)

#specify fill colors to use
fill_cols <- c('red', 'pink', 'blue', 'green', 'purple', 'brown')

#create bar plot to visualize values in vector
barplot(data, col=fill_cols)

#add legend to bottom of plot
legend('bottom', fill=fill_cols, legend=c('A', 'B', 'C', 'D', 'E', 'F'),
        horiz=TRUE, inset=c(0, -.1), xpd=TRUE)

horizontal plot in base R

Notice that a horizontal legend has been created and is placed at the bottom of the plot.

Feel free to play around with the values in the inset argument to adjust the location of the legend as well.

For example, we can make the y-value for the inset argument even more negative to push the legend even lower down:

#create vector of values
data <- c(4, 10, 7, 5, 4, 3)

#specify fill colors to use
fill_cols <- c('red', 'pink', 'blue', 'green', 'purple', 'brown')

#create bar plot to visualize values in vector
barplot(data, col=fill_cols)

#add legend to bottom of plot
legend('bottom', fill=fill_cols, legend=c('A', 'B', 'C', 'D', 'E', 'F'),
        horiz=TRUE, inset=c(0, -.2), xpd=TRUE)

Notice that the horizontal legend has been pushed even lower down below the plot.

Example 2: Use ncol Argument to Create a Horizontal Legend in Base R

The following code shows how to use the ncol argument within the legend() function to create a horizontal legend with three columns on the bottom of a plot in base R:

#create vector of values
data <- c(4, 10, 7, 5, 4, 3)

#specify fill colors to use
fill_cols <- c('red', 'pink', 'blue', 'green', 'purple', 'brown')

#create bar plot to visualize values in vector
barplot(data, col=fill_cols)

#add legend to bottom of plot
legend('bottom', fill=fill_cols, legend=c('A', 'B', 'C', 'D', 'E', 'F'),
        ncol=3, inset=c(0, -.15), xpd=TRUE)

Notice that a horizontal legend with three columns has been created and is placed at the bottom of the plot.

Feel free to change the value for the ncol argument to create a legend with a different number of columns.

The following tutorials explain how to perform other common tasks in R:

x