My question is closely related to R: Custom Legend for Multiple Layer ggplot , and to Format legend for multiple layers ggplot2 namely: I want to create custom legends for multiple-layer plot. However, there is a subtle difference:
In the original questions, the desired effect was to separate from two different groupping methods: fill
and color
and that's why it was possible to use two different scale_XXX
functions. In my case I create a plot
that contains points (one layer) and lines (second layer). Both layers are differentiated by color:
x <- seq(0, 10, .1)
y <- sin(x)
lbl <- ifelse(y > 0, 'positive', 'non-positive')
data.one <- data.frame(x=x, y=y, lbl=lbl)
data.two <- data.frame(x=c(0, 10, 0, 10), y=c(-0.5, -0.5, 0.5, 0.5), classification=c('low', 'low', 'high', 'high'))
plt <- ggplot(data.one) + geom_point(aes(x, y, color=lbl)) + scale_color_discrete(name='one', guide='legend')
plt <- plt + geom_line(data=data.two, aes(x, y, color=classification)) + scale_color_discrete(name='two', guide='legend')
print(plt)
Here is the result:
What I want is to separate the legends for points and lines, so that the legend looks like this:
I could not find a way to adopt the approach of the cited questions to my situation. Any ideas?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…