Turning off some legends in a ggplot
You can use guide = "none"
in scale_..._...()
to suppress legend.
For your example you should use scale_colour_continuous()
because length
is continuous variable (not discrete).
(p3 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
scale_colour_continuous(guide = "none") +
geom_point()
)
Or using function guides()
you should set "none"
for that element/aesthetic that you don't want to appear as legend, for example, fill
, shape
, colour
.
p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
geom_point()
p0+guides(colour = "none")
UPDATE
Both provided solutions work in new ggplot2
version 3.3.5 but movies
dataset is no longer present in this library. Instead you have to use new package ggplot2movies
to check those solutions.
library(ggplot2movies)
data(movies)
mov <- subset(movies, length != "")
Remove extra legends in ggplot2
Aesthetics can be set or mapped within a ggplot
call.
- An aesthetic defined within
aes(...)
is mapped from the data, and a legend created. - An aesthetic may also be set to a single value, by defining it outside
aes()
.
In this case, it appears you wish to set alpha = 0.8
and map colour = group
.
To do this,
Place the alpha = 0.8
outside the aes()
definition.
g <- ggplot(df, aes(x = x, y = y, group = group))
g <- g + geom_line(aes(colour = group))
g <- g + geom_point(aes(colour = group), alpha = 0.8)
g
For any mapped variable you can supress the appearance of a legend by using guide = 'none'
in the appropriate scale_...
call. eg.
g2 <- ggplot(df, aes(x = x, y = y, group = group)) +
geom_line(aes(colour = group)) +
geom_point(aes(colour = group, alpha = 0.8))
g2 + scale_alpha(guide = 'none')
Which will return an identical plot
EDIT
@Joran's comment is spot-on, I've made my answer more comprehensive
Removing one of two ggplot legends
Use the following code
ggplot(df, aes(x=factor(IR), y=misclass, colour = as.factor(resample),group=as.factor(resample))) +
geom_line(lwd=1) +
geom_point(shape=24,size=3,aes(color=as.factor(resample),fill=as.factor(resample)), show.legend = F) +hw+
scale_y_continuous(breaks=seq(0,0.15,0.025))+
scale_x_discrete(breaks=2^(0:7),labels=c("1","2","4","8","16","32","64","128"))+
labs(x="Imbalance Ratio",y="Risk")+
guides(colour=guide_legend(title="Resampling\nTechnique"))
I have added , show.legend = F
in geom_point
to remove the extra legend.
How do I just remove one of the two legends in GGPLOT
Set your fill
guide to FALSE
ggplot(ggdata) +
geom_point(aes(x=PC1, y=PC2, color=factor(Species)), size=5, shape=20) +
stat_ellipse(aes(x=PC1,y=PC2, fill=factor(Species)),
geom="polygon", level=0.95, alpha=0.2)+
guides(color=guide_legend("Species"), fill = FALSE)
Remove legend in ggplot
I couldn't run your code, but I rebuilt it with iris
.
Like Matt suggested, one thing would be, remove fill=F
:
ggplot(data=iris, aes(x = SepalLength , y = PetalLength, group=Name)) +
geom_point() +
geom_line() +
stat_smooth(method = "loess",se = T, aes(color=Name)) +
geom_ribbon(aes(ymin = 1, ymax = 3),alpha = 1/5) +
scale_x_continuous(breaks = 0:2, labels = c(0, 6, 12))
Or if you need it for some reason, use guides(fill="none")
:
ggplot(data=iris, aes(x = SepalLength , y = PetalLength, group=Name)) +
geom_point() +
geom_line() +
stat_smooth(method = "loess",se = T, aes(color=Name)) +
geom_ribbon(aes(ymin = 1, ymax = 3, fill=FALSE),alpha = 1/5) +
scale_x_continuous(breaks = 0:2, labels = c(0, 6, 12)) +
guides(fill="none")
Output:
ggplot2 has two figure legends. How to remove the top one?
You can specify show.legend = FALSE
in geom_linerange
:
library(ggplot2)
ggplot(df1, aes(x=Metabolite, y=OR, ymin=CI_low, ymax=CI_high,col=Method,fill=Method)) +
#specify position here
geom_linerange(size=1,position=position_dodge(width = 0.5), show.legend = FALSE) +
geom_hline(yintercept=1, lty=2) +
#specify position here too
geom_point(size=3, shape=21, colour="white", stroke = 0.5,position=position_dodge(width = 0.5)) +
scale_fill_manual(values=barCOLS)+
scale_color_manual(values=dotCOLS)+
scale_x_discrete(name="Metabolite") +
scale_y_continuous(name="Odds ratio (95% CI)", limits = c(0.4, 2)) +
coord_flip() +
theme_minimal()
Removing space in legend (ggplot)
You could remove the white space between the legend entries and background box by setting legend.spacing.y = unit(0, "pt")
. If that is still too much white space you could try with a negative value or reduce the top margin via legend.margin
:
Making use of ggplot2::economics_long
as example data:
library(ggplot2)
ggplot(subset(economics_long, variable != "pop"), aes(x = date, y = value)) +
geom_line(aes(linetype=variable)) +
scale_linetype_manual(values=c("solid", "twodash", "dotted", "solid", "twodash")) +
theme_classic() +
theme(legend.position = c(0.15, 0.85),
legend.background = element_rect(fill="white",
size=0.5,
linetype="solid",
colour ="gray"),
legend.spacing.y = unit(0, "pt"),
text = element_text(size=20),
legend.text = element_text(size=20),
axis.title=element_text(size=20),
legend.title = element_blank(),
panel.grid.major.y = element_line(),
panel.grid.minor.y = element_line())
Related Topics
Long/Bigint/Decimal Equivalent Datatype in R
What Is the Purpose of Setting a Key in Data.Table
Read Multiple CSV Files into Separate Data Frames
Create a Group Number For Each Consecutive Sequence
What Is the Width Argument in Position_Dodge
Merging Two Data Frames Using Fuzzy/Approximate String Matching in R
Starting Shiny App After Password Input
How to Format a Number as Percentage in R
Pasting Two Vectors With Combinations of All Vectors' Elements
How to Flatten/Merge Overlapping Time Periods
Turning Off Some Legends in a Ggplot
Capitalize the First Letter of Both Words in a Two Word String
What Are the "Standard Unambiguous Date" Formats For String-To-Date Conversion in R