I am having problems with a vanishing legend in ggpairs
.
I add a legend inside the top part of a lower triangular ggpairs plot as follows.
First I create a ggpairs plot without legend then I strip the legend I want from and ad hoc 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.
# 1 produce graph without legend
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 if I change some aspect of the theme the legend disappears.
graph2 <- graph1 +theme(strip.background =element_blank(),
strip.placement = "outside")
show(graph2)
I have asked this question in stackoverflow and got a soluction using the grid
package, I just wonder since the problems is with theme, if there is a solution within ggplot2
and ggpairs
.