Insert Layer underneath existing layers in ggplot2 object
Asked Answered
S

2

42

Given an Existing plot object is it possible to add a layer UNDERNEATH an existing layer?

Example, in the graph below, is it possible to add geom_boxplot() to P such that the boxplot appears underneath geom_point()?

## Starting from: 
library(ggplot2)
P <- ggplot(data=dat, aes(x=id, y=val)) + geom_point()

## This adds boxplot, but obscures some of the points
P + geom_boxplot()

Expected Output:

# Which is essentially
ggplot(data=dat, aes(x=id, y=val)) + geom_boxplot() + geom_point()
## However, this involves re-coding all of P (after the point insertion of the new layer).
##   which is what I am hoping to avoid. 

Example Output


Bonus question: If there are multiple layers in the existing plot, is it possible to indicate where specifically to insert the new layer (with respect to the existing layers)?


SAMPLE DATA

set.seed(1)
N <- 100
id <- c("A", "B")
dat <- data.frame(id=sample(id, N, TRUE), val=rnorm(N))
Sayer answered 27/11, 2013 at 17:54 Comment(5)
I don't think this is possible unless you save the lowest common layer and remix from there.Virgilio
@Maiasaura, thanks. That is what I am doing right now, but the original P is rather involved, and I am hoping to avoid recoding it for each small modification I need to produce.Sayer
you can easily change the order of the layers in the final plot, p2$layers = rev(p2$layers)Bicarb
@baptiste.. I don't want to rev alll of the layers, but thanks for pointing in the right direction!Sayer
you get the idea, it's just a listBicarb
S
62

Thanks @baptiste for pointing me in the right direction. To insert a layer underneath all other layers, just modify the layers element of the plot object.

## For example:
P$layers <- c(geom_boxplot(), P$layers)

Answer to the Bonus Question:

This handy little function inserts a layer at a designated z-level:

insertLayer <- function(P, after=0, ...) {
  #  P     : Plot object
  # after  : Position where to insert new layers, relative to existing layers
  #  ...   : additional layers, separated by commas (,) instead of plus sign (+)

      if (after < 0)
        after <- after + length(P$layers)

      if (!length(P$layers))
        P$layers <- list(...)
      else 
        P$layers <- append(P$layers, list(...), after)

      return(P)
    }
Sayer answered 27/11, 2013 at 18:23 Comment(1)
Does this work for theme() type elements?? (I ask, because simply adding them didn't work, so I wasn't sure if there was a slight variation needed to your approach for those types of element controls)...Teishateixeira
D
10

Expanding on Ricardo' answer, I use the following snippet:

library(ggplot2)

`-.gg` <- function(plot, layer) {
    if (missing(layer)) {
        stop("Cannot use `-.gg()` with a single argument. Did you accidentally put - on a new line?")
    }
    if (!is.ggplot(plot)) {
        stop('Need a plot on the left side')
    }
    plot$layers = c(layer, plot$layers)
    plot
}

As it allows you to:

P <- ggplot(data=dat, aes(x=id, y=val)) + geom_point()

P - geom_boxplot()

And the boxplot will be placed below the points.

Distrain answered 22/9, 2020 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.