Create Legend in ggplot2 Plot in R Last Updated : 06 Jun, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to create a legend in ggplot using 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. Method 1: Specifying col in ggplot() 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")) ) ggplot(df,aes(x,values,col=fun))+geom_line() Output: Method 2: Using col in geom The same can be done inside any geom function. In the example given below, it has been applied to geom_line() but the same can be done with any other geom function as per requirement. Syntax: geom_function(aes(col="name of the column to differentiate upon")) 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")) ) ggplot(df, aes(x, values)) + geom_line(aes(col = fun)) Output: Comment More infoAdvertise with us Next Article Create Legend in ggplot2 Plot in R V vanshikagoyal43 Follow Improve Article Tags : R Language R-ggplot Similar Reads Draw ggplot2 Legend without Plot in R A legend in the graph describes each part of the plot individually and is used to show statistical data in graphical form. In this article, we will see how to draw only the legend without a plot in ggplot2. First, let us see how to draw a graph with a legend so that the difference is apparent. For 3 min read Divide Legend of ggplot2 Plot in R In this article, we will discuss how to divide the legend of ggplot2 plot in the R programming language. To divide the legend of the ggplot2 plot, the user needs to install and import the gridExtra and cowplot packages in the R console. gridExrta package: Provides a number of user-level functions to 2 min read Create interactive ggplot2 graphs with Plotly in R "A Picture is worth a thousand words," and that picture would be even more expressive if the user could interact with it. Hence the concept of "interactive graphs or charts. Interactive charts allow both the presenter and the audience more freedom since they allow users to zoom in and out, hover and 6 min read How to annotate a plot in ggplot2 in R ? In this article, we will discuss how to annotate functions in R Programming Language in ggplot2 and also read the use cases of annotate. What is annotate?An annotate function in R can help the readability of a plot. It allows adding text to a plot or highlighting a specific portion of the curve. Th 4 min read Change Theme Color in ggplot2 Plot in R A theme in ggplot2 is a collection of settings that control the non-data elements of the plot. These settings include things like background colors, grid lines, axis labels, and text sizes. we can use various theme-related functions to customize the appearance of your plots, including changing theme 4 min read Like