How to Move, Customize, or Hide Legends in ggplot2

Legends are critical to clarify category labels. However, their default position on the right side of the plot can compress horizontal plot coordinates. ggplot2 allows you to relocate, reformat, or hide legend elements using the `theme()` configuration layer.
In This Tutorial
0.Live Code: Legend Positioning
1.Basic Position Constants (bottom, top, left, right)
2.Placing Legends Inside the Plot
3.Removing or Hiding Specific Scale Legends
4.Wrapping and Formatting Labels
0. Live Code: Legend Positioning
Legend configurations. Customize parameters using Python below, or upload your data to run R directly.
1. Basic Position Constants
Relocate the legend to preset margins around the plot panel using `legend.position`:
R / ggplot2
# Move legend to the bottom (horizontal layout)
ggplot(df, aes(x, y, color = group)) +
geom_point() +
theme(legend.position = "bottom")
# Hide the entire legend
ggplot(df, aes(x, y, color = group)) +
geom_point() +
theme(legend.position = "none")2. Placing Legends Inside the Plot
Try it
Try it now: turn this method into your next figure
Apply the same approach to your own dataset and generate clean, publication-ready code and plots in minutes.
Open in Plotivy Analyze →Newsletter
Get a weekly Python plotting tip
One concise tip each week for cleaner, faster scientific figures. Built for researchers who publish.
For plots with empty regions (e.g. top left or bottom right), place the legend inside the plot coordinate box using coordinates between `c(0,0)` (bottom-left) and `c(1,1)` (top-right):
R / ggplot2
ggplot(df, aes(x, y, color = group)) +
geom_point() +
theme(
legend.position = c(0.15, 0.8), # place inside top-left
legend.background = element_rect(fill = "white", color = "gray80")
)3. Removing Specific Scale Legends
If you map variables to both color and size, but only want a legend for color, set the unwanted scale to `FALSE` in `guides()`:
R / ggplot2
ggplot(df, aes(x, y, color = group, size = count)) +
geom_point() +
guides(size = "none") # remove the size legend, keep color4. Wrapping and Formatting Labels
Wrap long category labels using the `stringr` library inside the scale functions:
R / ggplot2
library(stringr)
ggplot(df, aes(x, y, color = long_group_names)) +
geom_point() +
scale_color_discrete(labels = function(x) str_wrap(x, width = 10))Chart gallery
Explore related formats
Review visual formats.

Scatterplot
Displays values for two variables as points on a Cartesian coordinate system.
Sample code / prompt
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import pandas as pd
# Generate sample data
np.random.seed(42)
n_samples = 200
height = np.random.normal(170, 8, n_samples)
weight = height * 0.6 + np.random.normal(0, 8, n_samples) - 50
Line Graph
Displays data points connected by straight line segments to show trends over time.
Sample code / prompt
import matplotlib.pyplot as plt
import numpy as np
# Generate temperature data for 3 major US cities over 12 months
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
nyc = [30, 32, 40, 52, 65, 75, 82, 81, 74, 63, 50, 38]
miami = [65, 66, 70, 76, 82, 87, 90, 90, 87, 80, 72, 66]
chicago = [25, 27, 35, 48, 62, 72, 80, 79, 71, 60, 45, 32]
# Create figure with enhanced stylingMove Legends Instantly Online
Upload your data and describe the design. Plotivy writes the ggplot2 code and executes it instantly.
Related chart guides
Apply this tutorial directly in the chart gallery with ready-to-run prompts and examples.
Technique guides scientists read next
scipy.signal.find_peaks guide
Tune prominence and width parameters for robust peak extraction.
Savitzky-Golay smoothing
Reduce noise while preserving peak shape and position.
PCA visualization workflow
Move from high-dimensional measurements to interpretable components.
ANOVA with post-hoc brackets
Add statistically correct pairwise significance annotations.
Found this helpful? Share it with your network.
Experimental Physicist & Photonics Researcher
Hands-on experience in silicon photonics, semiconductor fabrication (DRIE/ICP-RIE), optical simulation, and data-driven analysis. Built Plotivy to help researchers focus on discoveries instead of data struggles.
More about the authorVisualize your own data
Apply the techniques from this article to your own datasets. Upload CSV, Excel, or paste data directly.