Custom legend in ggpairs disappears after changing theme

694 views Asked by At

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)

This produces a graph with the legend in the desired place.

ggpairs graph with legend before changing theme:

enter image description here

However if I change some aspect of the theme the legend disappears.

graph2 <- graph1 +theme(strip.background =element_blank(), strip.placement = "outside")
show(graph2)

Legend vanishes after changing theme:

enter image description here

2

There are 2 answers

1
TarJae On

Learned here: Legend using ggpairs

# first run your code until
graph2 <- graph1 +theme(strip.background =element_blank(), strip.placement = "outside")

# then run this code

colidx <- c(3,5,6,7)
for (i in 1:length(colidx)) {
  
  # Address only the diagonal elements
  # Get plot out of plot-matrix
  inner <- getPlot(graph2, i, i);
  
  # Add ggplot2 settings (here we remove gridlines)
  inner <- inner + theme(panel.grid = element_blank()) +
    theme(axis.text.x = element_blank())
  
  # Put it back into the plot-matrix
  graph2 <- putPlot(graph2, inner, i, i)
  
  for (j in 1:length(colidx)){
    if((i==1 & j==1)){
      
      # Move the upper-left legend to the far right of the plot
      inner <- getPlot(graph2, i, j)
      inner <- inner + theme(legend.position=c(length(colidx)-0.25,0.50)) 
      graph2 <- putPlot(graph2, inner, i, j)
    }
    else{
      
      # Delete the other legends
      inner <- getPlot(graph2, i, j)
      inner <- inner + theme(legend.position="none")
      graph2 <- putPlot(graph2, inner, i, j)
    }
  }
}

# then run this code
show(graph2)

enter image description here

1
Sam_9090 On

I had similar issue. I think you need to use library(grid). see my solution.

# plotwithoutlegend
plotwithoutlegend <- ggpairs(
  iris,
  columns=1:4,
  switch="both",
  upper="blank",
  mapping=aes(color = Species,
              shape= Species,
              fill=Species, 
              alpha=0.5)
)+
  theme(strip.background =element_blank(), strip.placement = "outside")


#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)


##### plot legend with plot
grid.newpage()
grid.draw(plotwithoutlegend)
vp = viewport(x=.9, y=.75, width=.35, height=.3) ## control legend position
pushViewport(vp)
grid.draw(mylegend)
upViewport()