1

this one boggles my mind for quite a while ...

I want to show two different legends for two different geoms (geom_bar) with different data.frames.

The first legend should have the title "border" (filled by border from df.1) and the second should have the title "product" (filled by product from df.2). Both data.frames have the column=category in common.

Can you shed some light?

Here is the example

#library(ggplot2)

df.1 <- data.frame(category=c("A","A","A","B","B","B"),
border=c("I","II","III","I","II","III"),
value=c(1,2,1,2,1,2)
)

df.2 <- data.frame(category=c("A","A","A","B","B","B"),
product=c("P1","P2","P3","P1","P2","P3"),
value=c(1,2,3,3,1,2)
)

ggplot()+
geom_bar(aes(x=category, y=value, fill=border), data=df.1, width=.3)+
geom_bar(aes(x=category, y=value, fill=product), data=df.2, position="dodge", width=.25)
Tom Martens
  • 746
  • 9
  • 18
  • This isn't two legends and I like joran`s solution better, but another possibility is `+ guides(fill = guide_legend(ncol = 2, title.hjust = 0.4)) + scale_fill_discrete("border product")` – Julius Vainora Dec 28 '12 at 00:09

1 Answers1

4

One aesthetic -> one legend is sort of a fundamental design principle in ggplot. You can (sort of) get around it, but it's difficult. One thing to try, that doesn't look too bad, is this:

ggplot()+
    geom_bar(aes(x=category, y=value, fill=border), data=df.1, width=.3)+
    geom_bar(aes(x=category, y=value, colour=product), data=df.2, position="dodge", width=.25,alpha = 0.5)

enter image description here

joran
  • 169,992
  • 32
  • 429
  • 468
  • I would suggest also that @jorans suggestion can be hepled along with `+ guides(colour=guide_legend(override.aes=list(fill=c("red","green","blue"))))` – user1317221_G Dec 28 '12 at 00:05
  • I think overriding the fill in the color legend is a very good idea. Personally, I might set it to white or transparent. Also, nudging the size up slightly so that the colored borders might be a bit more visible as well. – joran Dec 28 '12 at 00:22
  • Hi Joran, thanks for your advise ... and I also like the idea of overwriting the fill color in the legend. I have to admit that I'm not fully satisfied :-( I'm still trying ... What I want to accomplish can be found here: http://www.perceptualedge.com/articles/misc/Bullet_Graph_Design_Spec.pdf – Tom Martens Dec 29 '12 at 16:25
  • @TomMartens The example you link to is quite different than what you sketched out in your question. Those bullet graphs never place more than one bar within another shaded bar, which would remove the need for more than one legend. – joran Dec 29 '12 at 22:44
  • @Joran, I want to show a group of 3 "within" the shaded border bar, maybe you can call it misuse of the idea of Stephen Few. I think you are right, that there is no need for two legends, what I'm going to draw is a group of 3 products within the stacked bar. Each bar represents a specific figure and the meaning of different colors can be explained in another way. There are still 3 days left for a "perfect solution" :-) – Tom Martens Dec 30 '12 at 00:22