How to move or position a legend in ggplot2? - GeeksforGeeks

06 Jun, 2021
In this article, we will discuss how to control a legend position in ggplot using an R programming language. To draw a legend within ggplot the parameter col is used, it basically adds colors to the plot and these colors are used to differentiate between different plots. To depict what each color represents a legend is produced by ggplot. col attribute can be specified in 2 places.
Simply specifying on basis of which attribute colors should be differentiated to the col attribute within ggplot() will get the job done.
Syntax: ggplot(df, aes(x, y, col=”name of the column to differentiate on the basis of”))
Code:
R
library("ggplot2")  function1 <- function(x){x**2}function2 <- function(x){x**3}function3 <- function(x){x/2}function4 <- function(x){2*(x**3)+(x**2)-(x/2)}  df=data.frame(x = -2:2,              values=c(function1(-2 : 2),                       function2(-2 : 2),                       function3(-2 : 2),                       function4(-2 : 2)),              fun=rep(c("function1", "function2",                        "function3","function4")))  plot = ggplot(df,aes(x,values,col=fun))+geom_line()plot
Output:
Now, we will see How to move or change position of ggplot2 Legend like at Top, Bottom and left. For Moving the position of ggplot2 legend at any side of the plot, we simply add the theme() function to geom_point() function.
Syntax : theme(legend.position)
Parameter : In General, theme() function has many parameters to specify the theme of the plot but here we use only legend.position parameter which specify the position of Legend.
Return : Theme of the plot.
We can specify the value of legend.position parameter as left, right, top and bottom to draw legend at left, right, top and bottom side of plot respectively.
Here we will change the position of the legend at the bottom of the plot diagram.
Syntax: theme(legend.position = “bottom”)
Code:
Python3
# Bottom -> legend around the plotplot + theme(legend.position = "bottom")
Output:
Here we will change the position of the legend at the top of the plot diagram.
Syntax: theme(legend.position = “top”)
Code:
Python3
# top -> legend around the plotplot + theme(legend.position = "top")
Output:
Here we will change the position of the legend to the right of the plot diagram.
Syntax: theme(legend.position = “right”)
Code:
Python3
# Right -> legend around the plotplot + theme(legend.position = "right")
Output:
Here we will change the position of the legend to the left of the plot diagram.
Syntax: theme(legend.position = “left”)
Code:
Python3
# Left -> legend around the plotplot + theme(legend.position = "left")
Output:
Here we can use a numeric vector to plotting the legend. It basically works on X, Y coordinates, the value should be 0 to 1.
Syntax: theme(legend.position = c(x, y))
Code:
Python3
# legend around the plotplot + theme(legend.position = c(1, 0.2))
Output
Picked
R-ggplot
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
R - if statement
How to filter R dataframe by multiple conditions?
Plot mean and standard deviation using ggplot2 in R
Time Series Analysis in R