2

Using the legend.box feature of ggplot, I want to integrate multiple aes legends into one box. However, I cannot seem to figure out how to organize the legend box.

In this example:

library(ggplot2)

dat <- data.frame(x=c(1,2,3),y=c(5,7,3))

p <- ggplot() +
  geom_col(data=dat,aes(x,y,fill="test col"))   +
  geom_point(data=dat,aes(x,y,shape="test point")) +
  geom_line( data=dat,aes(x,y,linetype="test line")) +
  theme(legend.position="top",
        legend.justification = "center",
        legend.background = element_blank(),
        legend.box.background = element_rect(fill="gray90", size=.5, color=NA))

p gives a line (which I cannot find how to wrap over multiple lines).

p + theme(legend.box = "vertical") is currently the best solution I could find but is not satisfactory, as I get stuck with a legend that has as many lines as aes.

Can please someone advise on how to wrap the legend box?

Thanks in advance

Jon
  • 351
  • 2
  • 10
  • 1
    I don't think this is actually that easy, maybe [this answer](https://stackoverflow.com/a/13327793/3283824) gives some ideas.. – erc Aug 17 '18 at 12:00
  • @beetroot The idea was that I wanted to stay away from doing that so as to keep the legend.box.background... and having to manually move multiple aes legends is also quite cumbersome. Thanks for the suggestion and the link! – Jon Aug 17 '18 at 12:15

0 Answers0