'ggplot2 and grid.arrange, place legend below arranged plots

I am facing a problem when I arrange 2 ggplots next to each other using grid.arrange(). I want to place a legend evenly below both plots.

So I have (pseudocode):

 p1 <- ggplot(data = df, aes(group = name, color=as.factor(fac), y = ys, x= (xs))) +
    geom_point() +
    geom_line() +
    theme(legend.position="bottom")

 p2 <- ggplot(data = df, aes(group = name, color=as.factor(fac), y = ys, x= (xs))) +
    geom_point() +
    geom_line() +

  grid.arrange(p1,p2, ncol=2, widths=c(0.9,1))

Is there any possibility to arrange the legend so that it is positioned evenly below both plots? If I do it as above, the legend is (logically) placed below the first plot.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source