Remove Legend Title from ggplot2 Plot in R (Example)
In this R post you’ll learn how to delete a legend title in a ggplot2 plot.
The content looks as follows:
- Example Data
- Example: Create ggplot2 Scatterplot without Legend Title
- Video, Further Resources & Summary
Let’s jump right to the examples.
Example Data
In the example of this tutorial, we’ll use the following data frame as basement:
set.seed(864) # Create random data data <- data.frame(x = rnorm(100), y = rnorm(100), group = c(rep("A", 50), rep("B", 50)))
Furthermore, we need to install and load the ggplot2 package to RStudio:
install.packages("ggplot2") # Install and load ggplot2 library("ggplot2")
Now, we can draw a plot based on the functions of the ggplot2 package as shown below:
ggp <- ggplot(data, aes(x, y, col = group)) + # Create default ggplot2 geom_point() ggp # Print plot
Figure 1: ggplot2 Plot with Legend Title.
Figure 1 shows the graph that we have created with the previous R code. It’s a scatterplot representing two data groups. On the right side of the plot, you can see a legend and a legend title.
Example: Create ggplot2 Scatterplot without Legend Title
In case we want to remove a legend title from a ggplot2 graphic, we can use the theme function and the legend.title argument. Have a look at the following R syntax and the resulting image:
ggp + # Remove legend title theme(legend.title = element_blank())
Figure 2: ggplot2 Plot without Legend Title.
As you can see in Figure 2, we deleted the legend title from our graph.
Video, Further Resources & Summary
Do you need more explanations on the R codes of this article? Then I can recommend to watch the following video of my YouTube channel. I’m illustrating the topics of this tutorial in the video:
In addition, you might read some of the related articles of this website:
- Change Legend Title in ggplot2
- Add Common Legend to Combined ggplot2 Plots
- Create Legend in ggplot2 Plot
- R Graphics Gallery
- The R Programming Language
You learned in this tutorial how to delete the heading of a ggplot legend in the R programming language. If you have further questions, let me know in the comments.