4

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:

before

What I want is to separate the legends for points and lines, so that the legend looks like this:

after

I could not find a way to adopt the approach of the cited questions to my situation. Any ideas?

1
  • 1
    You may have a look at this post and comments therein, e.g. "ggplot2 by design will not allow multiple legends for the same aesthetic". Thus, solutions are likely to be hack-ish.
    – Henrik
    Nov 11, 2013 at 18:13

1 Answer 1

6

The following is a hack. It extracts the legends from temporary plots and then combines everything using grid.arrange.

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

n <- 4; cols <- hcl(h=seq(15, 375-360/n, length=n)%%360, c=100, l=65)

cols1 <- cols[4:3]
names(cols1) <-  c("positive", "non-positive")
plt_1 <- ggplot(data.one) + 
  geom_point(data=data.one,aes(x, y, color=lbl)) +
  scale_color_manual(values=cols1)


cols2 <- cols[1:2]
names(cols2) <-  c("high", "low")
plt_2 <- ggplot(data.one) + 
  geom_line(data=data.two, aes(x, y, color=classification)) +
  scale_color_manual(values=cols2)
  

mylegend_1<-g_legend(plt_1)
mylegend_2<-g_legend(plt_2)

plt <- ggplot(data.one) + 
  geom_point(data=data.one,aes(x, y, color=lbl)) +
  geom_line(data=data.two, aes(x, y, color=classification)) +
  scale_color_discrete(guide="none")

library(gridExtra)
grid.arrange(plt,
             arrangeGrob(mylegend_1, mylegend_2, nrow=6),
             ncol=2,widths=c(7,1))

enter image description here

You'd need to fiddle a bit more to get the justification as in your expected output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.