• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

How To Place Legend Inside the Plot with ggplot2?

datavizpyr · June 24, 2020 ·

Last updated on August 14, 2025

Need to save space or create more polished visualizations? This comprehensive guide shows you exactly how to place ggplot2 legend inside plot using multiple positioning methods, with ready-to-use code examples that work for any chart type.

Default legend placement outside the plot area can waste valuable space, especially in publications, dashboards, or presentations where every inch matters. Moving the legend inside your plot creates cleaner, more compact visualizations while maintaining all the essential information your audience needs.

In this tutorial, you’ll master legend position ggplot2 techniques using theme(), coordinate-based positioning, and advanced customization options. Whether you’re creating scatter plots, bar charts, or complex multi-layered visualizations, these methods will give you complete control over legend placement. We will use ggplot2’s theme() function and legend.position argument to put the legend inside the plot.

Let us load tidyverse and load gapminder data for making a scatter plot with legend.

library(tidyverse)
Simple Scatter Plot with Legend
Simple Scatter Plot with Legend

How to Move Legend to inside plot in ggplot2?

We can move the ggplot2 legend inside the plot, when there is empty space inside. We can specify the location of legend using ggplot2 function theme(). Here we specify legend.position = c(0.87,0.25) to place the legend inside. The tuple basically specifies the x and y position in terms of the plot size in unit scale.

gapminder %>%
  ggplot(aes(gdpPercap,lifeExp, color=continent)) +
  geom_point() +
  scale_x_log10()+
  theme(legend.position = c(0.87, 0.25))
How To Place Legend Inside the Plot in ggplot2?
How To Place Legend Inside the Plot in ggplot2?

How to Move Legend to inside plot and Customize Legend Background in ggplot2?

In addition to placing the legend inside, we can also customize the legend box using legend.background argument inside theme() function. Here we make the legend box filled with white color and black color outline.

gapminder %>%
  ggplot(aes(gdpPercap,lifeExp, color=continent)) +
  geom_point() +
  scale_x_log10()+
  theme(legend.position = c(0.87, 0.25),
        legend.background = element_rect(fill = "white", color = "black"))
Customizing Legend Inside Scatter Plot ggplot2
Customizing Legend Inside Scatter Plot ggplot2

Explore the Complete ggplot2 Guide

35+ tutorials with code: scatterplots, boxplots, themes, annotations, facets, and more—tested and beginner-friendly.

Visit the ggplot2 Hub → No fluff—just code and visuals.

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scree plot: barplot with geom_col()How To Make Scree Plot in R with ggplot2 Grouped Boxplot with jittered points using position_jitterdodge()How to Make Grouped Boxplot with Jittered Data Points in ggplot2 Sinaplot and ViolinplotSinaplot vs Violin plot: Why Sinaplot is better than Violinplot

Filed Under: ggplot2, R Tagged With: ggplot2 move legend to inside

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

Copyright © 2026 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version