Placing the legend position at the bottom
Here, we will change the position of the legend and place it at the bottom. We will be using the theme() function and we will pass the legend.position argument. The legend.position argument will be assigned with the bottom.
Syntax :
theme( legend.position = “bottom”, …, complete = FALSE, validate = TRUE)
Example: R program to place the legend at the bottom
R
# Installing and loading the package library (ggplot2) # Read the dataset data ( "USArrests" ) # Plot the data plt <- ggplot (USArrests, aes (Murder, Assault, colour = UrbanPop)) + geom_point () # For placing the legend position as bottom plt + theme (legend.position = "bottom" ) |
Output :
Working with Legends in R using ggplot2
A legend in a plot helps us to understand which groups belong to each bar, line, or box based on its type, color, etc. We can add a legend box in R using the legend() function. These work as guides. The keys can be determined by scale breaks. In this article, we will be working with legends and associated customization using ggplot2 in R programming language.
Syntax :
legend(x, y = NULL, legend, fill = NULL, col = par(“col”), border = “black”, lty, lwd, pch)