remove legend title in ggplot
You were almost there : just add theme(legend.title=element_blank())
ggplot(df, aes(x, y, colour=g)) +
geom_line(stat="identity") +
theme(legend.position="bottom") +
theme(legend.title=element_blank())
This page on Cookbook for R gives plenty of details on how to customize legends.
How to change legend title in ggplot
This should work:
p <- ggplot(df, aes(x=rating, fill=cond)) +
geom_density(alpha=.3) +
xlab("NEW RATING TITLE") +
ylab("NEW DENSITY TITLE")
p <- p + guides(fill=guide_legend(title="New Legend Title"))
(or alternatively)
p + scale_fill_discrete(name = "New Legend Title")
How do I remove superfluous legend (titles) in ggplot?
Add scale_linetype(guide = FALSE)
:
ggplot(df, aes(time, value, group=1)) +
geom_line(aes(colour = series, linetype=series)) +
labs(x= "Locations", y="APC", colour= "New legend title") +
scale_x_reverse(breaks = c(100, 75, 55, 35, 10, -20),
labels = serlabel, expand=c(0,0))+
scale_linetype(guide = FALSE) +
theme_bw()
How can I remove the legend title in ggplot2?
You can modify the legend title by passing it as the first parameter to a scale. For example:
ggplot(carrots, aes(y=MeanLength, x=Farm, fill=Type)) +
geom_bar(position="dodge") +
theme(legend.position="top", legend.direction="horizontal") +
scale_fill_discrete("")
There is also a shortcut for this, i.e. labs(fill="")
Since your legend is at the top of the chart, you may also wish to modify the legend orientation. You can do this using opts(legend.direction="horizontal")
.
Removing legend name
Here's an example of how to alter the legend title and labels. To remove the title completely, you can set name = NULL
.
# Create data frame
df <- read.table(text = "X Y Variable
0.351 4.453 a
0.352 4.423 a
0.353 4.422 a
0.351 5.656 b
0.352 5.431 b
0.353 5.222 b", header = TRUE)
# Load libraries
library(ggplot2)
# Plot results
ggplot(data=df, aes(x=df$X, y=df$Y, color=df$Variable))+
geom_line(size=1)+
labs(x = "x",
y = "y") +
scale_color_manual(values=c("green","red"),
breaks=c("a","b"),
labels = c("Label one", "Label two"),
name = "My Legend Title") +
theme_bw()
Created on 2019-04-25 by the reprex package (v0.2.1)
Legend title in ggplot2
You can rename aesthetics in legends for example via the ggplot2 function labs
like in the following reprex.
library(ggplot2)
df <- data.frame(
x = runif(100),
y = runif(100),
z1 = rnorm(100),
z2 = abs(rnorm(100))
)
ggplot(df, aes(x, y)) +
geom_point(aes(colour = z1)) +
scale_colour_gradient2() +
labs(colour = "My Legend Name")
reprex output
Created on 2021-10-13 by the reprex package (v2.0.1)
Related Topics
Loop Over Rows of Dataframe Applying Function with If-Statement
Read Lines by Number from a Large File
HTML with Multicolumn Table in Markdown Using Knitr
Street Address to Geolocation Lat/Long
Rmarkdown: Pandoc: PDFlatex Not Found
Writing Functions VS. Line-By-Line Interpretation in an R Workflow
How Exactly Does R Parse '->', the Right-Assignment Operator
How to Subset from a List in R
Grid Line Consistent with Ticks on Axis
How to Control the Igraph Plot Layout with Fixed Positions
How to Separate Title Page and Table of Content Page from Knitr Rmarkdown PDF
Ggally::Ggpairs Plot Without Gridlines When Plotting Correlation Coefficient
Download Attachment from an Outlook Email Using R
Change the Color of Action Button in Shiny
Label Minimum and Maximum of Scale Fill Gradient Legend with Text: Ggplot2
R: How Does a Foreach Loop Find a Function That Should Be Invoked