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
I intended to color lines in pink, and points in yellow. I don't want to use colour argument in respective geom(), I want to use scale to change colour.
p3 <- ggplot(dfcc,aes(x = yr, y = mean)) +
geom_line(aes(color = '')) +
geom_point(aes(color = ''))
p3 + scale_colour_manual(values =c('pink', 'yellow'))
This gives this plot, both lines and points are not in the right colours.
Hence, I have two questions
can I use "scale_colour_manual" to change the line and point colors in one go?
if having multiple geoms and multiple scales, how does the system know which scale applies to which geom?
Any help and explanation would be much appreciated!
Use package ggnewscale.
set.seed(2022)
df1 <- data.frame(x = 1:20, y = cumsum(rnorm(20, 2)))
library(ggplot2)
ggplot(df1, aes(x, y)) +
geom_line(color = "pink", linewidth = 2) +
ggnewscale::new_scale_color() +
geom_point(color = "yellow", size = 3) +
theme_classic()
Created on 2022-12-25 with reprex v2.0.2
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
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
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)
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")