I add a legend inside the top part of a lower triangular ggpairs with putPlot() as follows:
First I create a ggpairs plot without legend then I strip the legend I want from and ad hoc auxiliary graph and place in the ggpairs plot it with putPlot. It works nicely until I try to modify the theme which makes the legend disappear.
library(GGally)
library(ggplot2)
plotwithoutlegend <-ggpairs(
iris,
columns=1:4,
switch="both",
upper="blank",
mapping=aes(color = Species,
shape= Species,
fill=Species,
alpha=0.5)
)
#2 grab the legend from a graph with the legend I want (without alpha).
auxplot <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width,
color=Species,
shape=Species,
fill=Species)) + geom_point()
mylegend <- grab_legend(auxplot)
# 3 place the legend in the ggpairs grid with putPlot
graph1 <- putPlot(plotwithoutlegend,mylegend,3,4)
show(graph1)
# However adding a theme modification makes the legend disappear.
graph2 <- graph1 +theme(strip.background =element_blank(),
strip.placement = "outside")
show(graph2)
I add a legend inside the top part of a lower triangular ggpairs with putPlot() as follows: First I create a ggpairs plot without legend then I strip the legend I want from and ad hoc auxiliary graph and place in the ggpairs plot it with putPlot. It works nicely until I try to modify the theme which makes the legend disappear.