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


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.

Additional Resources

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

How to Change Legend Size in Base R Plot
How to Draw a Legend Outside of a Plot in R
How to Change Legend Position in Base R Plots

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *