• 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
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Place Legend Inside the Plot with ggplot2?

datavizpyr · June 24, 2020 ·

By default, when we make a plot with legend using ggplot2, it places the legend outside of the plot on the right side. Sometimes one might want to place the legend inside the plot. One of the advantages of placing it inside is that we may gain additional space for the plot.

In this post, we will learn how to adjust the legend position and place the legend position inside the plot. 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

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 Sinaplot and ViolinplotSinaplot vs Violin plot: Why Sinaplot is better than Violinplot Visualizing Missing Data with Barplot in R ggplot2Visualizing Missing Data with Barplot in R

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

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

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

Go to mobile version