ggobi / ggally

R package that extends ggplot2
http://ggobi.github.io/ggally/
588 stars 118 forks source link

Custom legend in ggpairs dissapears after change of theme. #417

Open sardanza opened 3 years ago

sardanza commented 3 years ago

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)
schloerke commented 3 years ago

From community question https://community.rstudio.com/t/custom-legend-in-ggpairs-disappears-after-changing-theme/104111/2