Hiding legend title

We will hide the legend title here using the theme() function providing legend.title as an argument. Here, the legend.title is equal to element_blank() function. The element_blank() assigns no space and also draws nothing.

Syntax : 

theme( legend.title = element_blank(),…., complete = FALSE, validate = TRUE)

Example: R program to hide legend title

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()
  
# Hiding legend title
plt + theme(legend.title = element_blank())


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)

Similar Reads

Installation

We will first install and load the ggplot2 package since, without this package, we would not be able to plot our data. So, we can install the package using the below command....

Changing the color and size of the title of the legend

...

Changing the color and size of the labels

Here, we will change the color and size of the title of the legend formed from the plot. This can be done using the theme() function. We will pass the legend.title argument inside this function. The legend.title argument basically refers to the title of the legend formed. The legend.title argument is equal to the element_text function which is inherited from the title and accepts arguments like color, size, etc....

Hiding legend title

...

Removing legend

Here, we will change the color and size of the labels of the legend formed. This can be done using the theme() function by passing legend.text as the argument. The legend.text is equal to element_text() function which is inherited from text and accepts arguments like color, size, font, etc. The legend.text argument basically refers to labels of legend items....

Placing the legend position at the top

...

Placing the legend position at the bottom

We will hide the legend title here using the theme() function providing legend.title as an argument. Here, the legend.title is equal to element_blank() function. The element_blank() assigns no space and also draws nothing....

Drawing a box around legend and adding a background color to it

...

Changing key style and changing legend color

We can remove the legend by providing “none” in legend.position as an argument of theme() function. The legend.position argument refers to the position of the legends....

Contact Us