Change Location of Barchart Legend in R (Example Code)

In this tutorial you’ll learn how to modify the position of the legend of a bargraph in the R programming language.

Creation of Example Data

my_mat <- matrix(24:10, ncol = 3)    # Create matrix in R
rownames(my_mat) <- letters[1:5]
my_mat                               # Display example matrix in RStudio console
#   [,1] [,2] [,3]
# a   24   19   14
# b   23   18   13
# c   22   17   12
# d   21   16   11
# e   20   15   10

Example: Specify Position of Barchart Legend Properly

barplot(my_mat,                      # Create barplot with legend
        col = 1:nrow(my_mat),
        legend.text = TRUE,
        args.legend = list(x = "topleft",
                           bty = "n",
                           inset = c(- 0.22, 0)))

r graph figure 1 change location barchart legend

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top