rggplot2ggridgesgeom-segment

How can I make this ggplot render more quickly?


Below is a reprex of the data I'm working with. The geom_segment calls make the rendering very sluggish. Is there an alternate way to achieve the same result more quickly?

library(ggplot2)
library(ggridges)

n <- 5000; l <- c(2, 5, 7, 9); sd_27 <- c(5.9, 11, 14, 17)
df <- data.frame(name = c(rep("A", n), rep("B", n), 
                          rep("C", n), rep("D", n)),
                 value = c(rpois(n, l[1]), rpois(n, l[2]),
                           rpois(n, l[3]), rpois(n, l[4])))

ggplot(df, aes(x = value, y = name, fill = name)) + geom_density_ridges(alpha = 0.8) +
  geom_segment(aes(x = l[[1]], y = "A", xend = l[[1]], yend = 2, color = "mean")) +
  geom_segment(aes(x = l[[2]], y = "B", xend = l[[2]], yend = 3, color = "mean")) +
  geom_segment(aes(x = l[[3]], y = "C", xend = l[[3]], yend = 4, color = "mean")) +
  geom_segment(aes(x = l[[4]], y = "D", xend = l[[4]], yend = 5, color = "mean")) +
  geom_segment(aes(x = sd_27[[1]], y = "A", xend = sd_27[[1]], yend = 2, color = "sd_27")) +
  geom_segment(aes(x = sd_27[[2]], y = "B", xend = sd_27[[2]], yend = 3, color = "sd_27")) +
  geom_segment(aes(x = sd_27[[3]], y = "C", xend = sd_27[[3]], yend = 4, color = "sd_27")) +
  geom_segment(aes(x = sd_27[[4]], y = "D", xend = sd_27[[4]], yend = 5, color = "sd_27"))

enter image description here


Solution

  • Instead of adding each segment via a separate geom_segment layer you could put all the data for the segments in one data frame and add the segments via one geom_segment which according to microbenchmark reduces the rendering time to about one fifth:

    geom_segment

    library(ggplot2)
    library(ggridges)
    
    set.seed(42)
    
    n <- 5000; l <- c(2, 5, 7, 9); sd_27 <- c(5.9, 11, 14, 17)
    df <- data.frame(name = c(rep("A", n), rep("B", n), 
                              rep("C", n), rep("D", n)),
                     value = c(rpois(n, l[1]), rpois(n, l[2]),
                               rpois(n, l[3]), rpois(n, l[4])))
    
    dl <- data.frame(x = l, y = LETTERS[1:4], yend = 2:5, color = "mean")
    dsd <- data.frame(x = sd_27, y = LETTERS[1:4], yend = 2:5, color = "sd_27")
    
    d <- do.call(rbind, list(dl, dsd))
    
    p1 <- function() {
      ggplot(df, aes(x = value, y = name, fill = name)) + 
        geom_density_ridges(alpha = 0.8) +
        geom_segment(data = d, aes(x = x, y = y, xend = x, yend = yend, color = color), inherit.aes = FALSE)
    }
    
    p2 <- function() {
      ggplot(df, aes(x = value, y = name, fill = name)) + geom_density_ridges(alpha = 0.8) +
        geom_segment(aes(x = l[[1]], y = "A", xend = l[[1]], yend = 2, color = "mean")) +
        geom_segment(aes(x = l[[2]], y = "B", xend = l[[2]], yend = 3, color = "mean")) +
        geom_segment(aes(x = l[[3]], y = "C", xend = l[[3]], yend = 4, color = "mean")) +
        geom_segment(aes(x = l[[4]], y = "D", xend = l[[4]], yend = 5, color = "mean")) +
        geom_segment(aes(x = sd_27[[1]], y = "A", xend = sd_27[[1]], yend = 2, color = "sd_27")) +
        geom_segment(aes(x = sd_27[[2]], y = "B", xend = sd_27[[2]], yend = 3, color = "sd_27")) +
        geom_segment(aes(x = sd_27[[3]], y = "C", xend = sd_27[[3]], yend = 4, color = "sd_27")) +
        geom_segment(aes(x = sd_27[[4]], y = "D", xend = sd_27[[4]], yend = 5, color = "sd_27"))
    }
    
    # Check plot
    p1()
    #> Picking joint bandwidth of 0.381
    

    # Compare running time
    microbenchmark::microbenchmark(p1()) 
    #> Unit: milliseconds
    #>  expr      min       lq     mean   median      uq      max neval
    #>  p1() 1.859514 1.917135 2.162416 1.936781 2.42122 5.056147   100
    microbenchmark::microbenchmark(p2())
    #> Unit: milliseconds
    #>  expr     min       lq     mean   median       uq      max neval
    #>  p2() 9.37298 9.669749 10.20821 9.774624 10.17852 22.42459   100