Reputation: 441
I'm trying to make a separate legend for geom_vline
and geom_hline
in a ggplot with facets.
Here is my code:
set.seed(1)
vec1 = rnorm(20)
set.seed(2)
vec2 = rnorm(20)
set.seed(3)
vec3 = rnorm(20)
vec = c(vec1,vec2,vec3)
let = rep(sample(letters[1:3],20,replace=TRUE),3)
num = c(rep(1:20,3))
lab = c(rep("label 1",20),rep("label 2",20),rep("label 3",20))
# Vertical lines for geom_vline
line1 = seq(1,20,5)
df = data.frame(vector = vec, index = num, name = let, label = lab)
# Define facet order
df$label_f = factor(df$label, levels=c("label 1","label 2","label 3"))
# Dataframe for horizontal lines
hl = data.frame(label = c("label 1","label 2","label 3"), Z = c(0.5,1,-0.5))
# Factor to seperate horizontal lines for facets
hl$label_f = factor(hl$label, levels=c("label 1","label 2","label 3"))
ggplot(data=df,aes(index,vector)) +
geom_point(aes(alpha = name, color=name)) +
geom_vline(xintercept=line1, color="blue") +
geom_hline(data = hl, aes(yintercept=Z),color="red") +
scale_alpha_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c(1,0.5,0.5)) +
scale_color_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c("black","orange","darkorange4")) +
xlab("index") + ylab("rnorm") +
facet_wrap(~label_f,ncol=1,scale="free_y") +
theme(legend.position="bottom")
I want a separate legend ("Legend 2") for the geom_hline
and geom_vline
with the appropriate labels and colours:
I want the geom_hline
to be horizontal and the geom_vline
vertical in the legend too!
This is what it should look like:
Thank you!
Upvotes: 2
Views: 923
Reputation: 24845
One option is to use fill=name
for the "color" of the geom_point(), rather than color=name
.
Additionally:
pch=21
aes()
(I additionally increased the size of the geom_points)
vline_data = data.frame(label_f = c(rep("label 1",length(line1)),rep("label 2",length(line1)),rep("label 3",length(line1))), Z = line1)
ggplot(data=df,aes(index,vector)) +
geom_point(aes(fill=name ), size=4,pch=21) +
geom_vline(data = vline_data, aes(xintercept=Z, color="blue")) +
geom_hline(data = hl, aes(yintercept=Z, color="red")) +
scale_fill_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c("black","orange","darkorange4")
) +
scale_alpha_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c(1,0.5,0.5)) +
scale_color_manual(name="Additional Lines",
labels=c("Vertical", "Horizontal"),
values=c("blue", "red")) +
xlab("index") + ylab("rnorm") +
facet_wrap(~label_f,ncol=1,scale="free_y") +
theme(legend.position="bottom") +
guides(alpha="none")
Upvotes: 2
Reputation: 125687
Making use of ggnewscale
and custom draw_key
function this could be achieved like so:
A separate legend for the lines could be achieved via ggnewscale::new_scale_color
which will add a second color legend. Doing so requires to add a mapping to both your hline
and your vline
.
Tricky part is that by default you will get both a vertical and a horizontal line for the legend key. To override that I added a custom key glyph function which conditions one the color
of the lines. That means that if you change the colors you have to adjust the condition too.
library(ggplot2)
# Custom Key Glyph. Vertical Line if color = "blue". Horizontal Line otherwise
draw_key_cust <- function(data, params, size) {
if (data$colour == "blue") {
draw_key_vpath(data, params, size)
} else {
draw_key_path(data, params, size)
}
}
ggplot(data=df,aes(index,vector)) +
geom_point(aes(alpha = name, color=name)) +
scale_alpha_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c(1,0.5,0.5), guide = guide_legend(order = 1)) +
scale_color_manual(name="Indices",
labels=c("legend label 1","legend label 2","legend label 3"),
values=c("black","orange","darkorange4"), guide = guide_legend(order = 1)) +
ggnewscale::new_scale_color() +
geom_vline(data = data.frame(xintercept = line1), aes(xintercept = xintercept, color="line1"), key_glyph = "cust") +
geom_hline(data = hl, aes(yintercept = Z, color="line2"), key_glyph = "cust") +
scale_color_manual(name="Legend 2",
labels=c(line1 ="line 1", line2 = "line 2"),
values=c(line1 = "blue", line2 = "red"),
guide = guide_legend(order = 2)) +
xlab("index") + ylab("rnorm") +
facet_wrap(~label_f,ncol=1,scale="free_y") +
theme(legend.position="bottom")
Upvotes: 3