How to get rid of the geom_text legend in ggplot? [duplicate] - r

I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
And I'm trying to make a plot:
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 = .8))
g
The result looks fine with one exception. It has an extra legend showing the alpha for my geom_point layer.
How can I keep the legend showing group colors, but not the one that shows my alpha settings?

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

Just add the show.legend = F code after the part where you don't want it.
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 = .8), show.legend = F)

Another simple option is using the function guides, which makes it possible to remove a particular aesthetic (fill, alpha, color) or multiple from the legend. Here is a reproducible example for removing only alpha legend and removing both alpha and color:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
library(ggplot2)
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 = .8))
# Remove legend alpha
g + guides(alpha = "none")
# Remove legend alpha and color
g + guides(alpha = "none", color = "none")
Created on 2022-08-21 with reprex v2.0.2

For old versions of ggplot2 (versions before 0.9.2, released in late 2012), this answer should work:
I tried this with a colour_scale and it did not work. It appears that the colour_scale_hue item works like a function with a default parameter TRUE. I added scale_colour_hue(legend=FALSE) and it worked.
I am not sure if this is the case for all color scale items in ggplot

Related

How do I remove this size label on my plot while increasing the point size? [duplicate]

I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
And I'm trying to make a plot:
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 = .8))
g
The result looks fine with one exception. It has an extra legend showing the alpha for my geom_point layer.
How can I keep the legend showing group colors, but not the one that shows my alpha settings?
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
Just add the show.legend = F code after the part where you don't want it.
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 = .8), show.legend = F)
Another simple option is using the function guides, which makes it possible to remove a particular aesthetic (fill, alpha, color) or multiple from the legend. Here is a reproducible example for removing only alpha legend and removing both alpha and color:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
library(ggplot2)
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 = .8))
# Remove legend alpha
g + guides(alpha = "none")
# Remove legend alpha and color
g + guides(alpha = "none", color = "none")
Created on 2022-08-21 with reprex v2.0.2
For old versions of ggplot2 (versions before 0.9.2, released in late 2012), this answer should work:
I tried this with a colour_scale and it did not work. It appears that the colour_scale_hue item works like a function with a default parameter TRUE. I added scale_colour_hue(legend=FALSE) and it worked.
I am not sure if this is the case for all color scale items in ggplot

Removing ggplot2 legend removes whole data from the plot

Here I have 2-dim numeric array dataset and numeric 1-dim array of labels clustring. Then I plot it with the following code:
s = data.frame(x = dataset[,1], y = dataset[,2])
p = ggplot(s, aes(x, y))
p + geom_point(aes(colour = factor(clustering)))
which displays beautiful picture:
Now I want to remove legend completely, so here I've found possible solution:
# Remove legend for a particular aesthetic (fill)
p + guides(fill=FALSE)
# It can also be done when specifying the scale
p + scale_fill_discrete(guide=FALSE)
# This removes all legends
p + theme(legend.position="none")
but none of such commands wont help. It shows empty plot instead:
So how do I remove the legend from my plot?
Try this:
library(ggplot2)
s = data.frame(x = rnorm(20), y = rnorm(20), clustering = rep(c(1, 2), 10))
p <- ggplot(s, aes(x, y))+
guides(fill=FALSE)+
geom_point(aes(colour = factor(clustering)))+
scale_fill_discrete(guide=FALSE)+
theme(legend.position="none")
p
In your code, you are not saving the plot again after each time you add something to it. You can fix this by changing the lines that add to the plot:
# Remove legend for a particular aesthetic (fill)
p = p + guides(fill=FALSE)
But the way I wrote is is more common R formatting.
Use show.legend = FALSE within geom_point. Here is an example using ggplot2's diamonds dataset.
s <- diamonds
p <- ggplot(data = s, aes(x = depth, y = price))
p + geom_point(aes(colour = factor(cut)), show.legend = FALSE)
Just try this:
p + geom_point(aes(colour = factor(clustering)),show.legend=FALSE)

alpha and fill legends in ggplot2 boxplots?

I'm trying to combine alpha and fill aesthetics. It works when I'm using geom_bar (or geom_points, for color), but the alpha legend doesn't work in when I'm using geom_boxplot.
library(data.table)
library(ggplot2)
dt = data.table(x = rep(1:5,6), y = rnorm(30),
tag1 = rep(c('hey', 'what'), 15),
tag2 = rep(c('yeah', 'yeah', 'so', 'so', 'so'), 6))
It works for bars:
ggplot(dt[, list(y = mean(y)), by = list(x, tag1, tag2)],
aes(x = x, y = y, fill = tag1, alpha = tag2,
group = interaction(x,tag1,tag2))) +
geom_bar(stat = 'identity', position = 'dodge')
But not for boxplot - the alpha legend is empty.
ggplot(dt, aes(x = x, y = y, fill = tag1, alpha = tag2,
group = interaction(x, tag1, tag2))) +
geom_boxplot()
A simpler version can be done with no fill - it seems like bar defaults to gray/lightgray, and boxplot defaults to white/lightwhite:
ggplot(dt[, list(y = mean(y)), by = list(x, tag2)],
aes(x = x, y = y, alpha = tag2,
group = interaction(x,tag2))) +
geom_bar(stat = 'identity')
ggplot(dt, aes(x = x, y = y, alpha = tag2,
group = interaction(x, tag2))) +
geom_boxplot()
But I'm not really sure how to fix this. Any thoughts?
I'm not sure why ggplot doesn't actually provide the alpha levels in the legend for boxplots, but you can hard code it using override.aes. (Editorial note: I find the alpha aesthetic a bit confusing for either the boxplot or the bar plot. It's hard to mentally separate the transparency from the fill color and the grey-scale alpha legend exacerbates the problem, because nothing is mapped to grey in the plot.)
In the code below, to improve visibility of the legend, I've removed the box lines from the alpha legend and increased the legend key height. I've also edited the aesthetics to remove the need for the group argument.
ggplot(dt, aes(x=factor(x), y=y, fill=tag1, alpha=tag2)) +
geom_boxplot() +
scale_alpha_manual(values=c(0.2,0.7)) +
guides(alpha=guide_legend(override.aes=list(fill=hcl(c(15,195),100,0,alpha=c(0.2,0.7)),
colour=NA))) +
theme(legend.key.height=unit(1,"cm"))
Another option would be to use interaction for both the fill and alpha aesthetics, but it turns out ggplot doesn't include any colors in that case:
ggplot(dt, aes(x=factor(x), y=y, alpha=interaction(tag1,tag2)),
fill=interaction(tag1,tag2)) +
geom_boxplot() +
scale_fill_manual(values=rep(hcl(c(15,195),100,65), 2)) +
scale_alpha_manual(values=rep(c(0.3, 1), each=2)) +
theme(legend.key.height=unit(2,"cm"))
So, instead you can do it all with the fill aesthetic, but include transparency in the colour specification. This works, but, once again, because transparency and color are somewhat intermingled in visual perception, it's probably better to just go with four different colors.
ggplot(dt, aes(x=factor(x), y=y, fill=interaction(tag1,tag2,sep="-"))) +
geom_boxplot() +
scale_fill_manual(values=hcl(c(15,195,15,195),100,65, alpha=c(0.4,0.4,1,1))) +
theme(legend.key.height=unit(1,"cm")) +
labs(fill="Tag 1 - Tag 2")

manually adding legend to ggplot with different layer types

Consider the graph below that I created with the code further below. I would like to add a legend that might say something like "median" and "90% confidence interval." I've seen this question partially addressed here (thanks Roland), but when I try to implement it in my own code, the legend looks silly because the middle line doesn't have a fill while the ribbon does. Is there some way to get the legend to look sensible, where it shows just a line for the middle line and a fill box for the ribbon?
library(ggplot2)
middle = data.frame(t=c(0,1,2,3),value=c(0,2,4,6))
ribbon = data.frame(t=c(0,1,2,3),min=c(0,0,0,0),max=c(0,4,8,12))
g = ggplot()
g = g + geom_line (data=middle,aes(x=t,y=value),color='blue',size=2)
g = g + geom_ribbon(data=ribbon,aes(x=t,ymin=min,ymax=max),alpha=.3,fill='lightblue')
print(g)
library(ggplot2)
middle = data.frame(t=c(0,1,2,3),value=c(0,2,4,6))
ribbon = data.frame(t=c(0,1,2,3),min=c(0,0,0,0),max=c(0,4,8,12))
g = ggplot()
g = g + geom_ribbon(data=ribbon,aes(x=t,ymin=min,ymax=max,fill="CI" ,color="CI"))
g = g + geom_line (data=middle,aes(x=t,y=value, color="median"))
g = g + scale_colour_manual(values=c("lightblue","blue"))
g = g + scale_fill_manual (values=c("lightblue"))
print(g)
First, set guide="none" for the scale_fill_manual() and then use function guides() with argument override.aes= to change linetype= and fill= according to line and confidence interval.
ggplot() +
geom_ribbon(data=ribbon,aes(x=t,ymin=min,ymax=max,fill="CI" ,color="CI")) +
geom_line(data=middle,aes(x=t,y=value,color="median"))+
scale_colour_manual("Legend",values=c("lightblue","blue")) +
scale_fill_manual(values=c("lightblue"),guide="none")+
guides(colour = guide_legend(override.aes = list(linetype=c(0,1),fill=c("lightblue","white"))))

Remove extra legends in ggplot2

I have a simple data frame that I'm trying to do a combined line and point plot using ggplot2. Supposing my data looks like this:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
And I'm trying to make a plot:
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 = .8))
g
The result looks fine with one exception. It has an extra legend showing the alpha for my geom_point layer.
How can I keep the legend showing group colors, but not the one that shows my alpha settings?
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
Just add the show.legend = F code after the part where you don't want it.
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 = .8), show.legend = F)
Another simple option is using the function guides, which makes it possible to remove a particular aesthetic (fill, alpha, color) or multiple from the legend. Here is a reproducible example for removing only alpha legend and removing both alpha and color:
df <- data.frame(x=rep(1:10,2), y=c(1:10,11:20),
group=c(rep("a",10),rep("b",10)))
library(ggplot2)
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 = .8))
# Remove legend alpha
g + guides(alpha = "none")
# Remove legend alpha and color
g + guides(alpha = "none", color = "none")
Created on 2022-08-21 with reprex v2.0.2
For old versions of ggplot2 (versions before 0.9.2, released in late 2012), this answer should work:
I tried this with a colour_scale and it did not work. It appears that the colour_scale_hue item works like a function with a default parameter TRUE. I added scale_colour_hue(legend=FALSE) and it worked.
I am not sure if this is the case for all color scale items in ggplot

Resources