Home > Back-end >  Large performance difference between ggplot2 on Mac and Windows
Large performance difference between ggplot2 on Mac and Windows

Time:10-03

I routinely generate sizable chart packs with ggplot2, and I observe that the performance is dramatically different (3x) between my Mac and Windows machines for my typical use case. In both cases I am using R 4.1 with ggplot2 3.3.5, the AGG graphics backend, and the anti-aliasing option set to Default in RStudio. The Mac machine is a Mac mini M1 running R for Apple Silicon. Unfortunately the Windows box is a virtual machine at work and it is hard to get the exact specs on it, but it has 32GB of RAM and I do not notice any slowness on other workloads so I feel like such a large discrepancy in performance on a straightforward task cannot easily be attributed to the RAM, processor, or disk speed.

Below is some code demonstrating a typical task with simulated data: ten PDF pages of facetted plots with 16 line plots per page. On the Mac the plots generate in about 3 seconds, while it takes 9-10 seconds on Windows.

library(data.table)
library(ggplot2)
library(glue)
theme_set(theme_bw())

dat <- list()
n = 51
i <- 1
for (linetype in 1:2) {
  for (color in 1:5) {
    for (v in 1:16) {
      dat[[i]] <- data.table(
        linetype=glue("L{linetype}"),
        color=glue("C{color}"),
        variable=glue("V{v}"),
        period=1:n,
        value=rnorm(n)
      )
      i <-i   1
    }
  }
}

dat <- rbindlist(dat)

system.time({
  pdf("test.pdf", onefile=TRUE)
  for (i in 1:10) {
    print(ggplot(dat, aes(period, value, color=color, linetype=linetype)) geom_line() facet_wrap(~variable))
  }
  dev.off()
}) 

CodePudding user response:

This is interesting, because I've experienced the opposite when it comes to ggplot2 rendering times if I'm drawing the plot to a graphics device window, and not saving to a file.

I share a task with a collague at work that involves generating a faceted ggplot with approximately 35-40 facets. We often have to generate this plot over several iterations as we make adjustments to certain parameters. On his Windows machine, the plot generates to a graphics device window in just a few seconds, where on my MacBook Pro (64GB M1) it takes almost a minute.

However, if I save the plot to a pdf, it generates in less than a second. This has led to me using a pdf viewer (Skim) that can refresh automatically on file update, and using ggsave to generate the image for review.

  • Related