title: Why does tmap render 80 times faster than ggplot2? [Plotting shapefiles in R with ggplot2::geom_sf(), using XQuartz/X11 graphics device on macOS]
body: <p><strong>Update/Edit/Reprex</strong>: Rendering the same spatial data with the same graphics device takes 1 second with <code>tmap</code> versus 80 seconds with <code>ggplot2</code>, even though the <code>tmap</code> plot's R object is 80x larger in size. Difference in internals and/or implementation btw. packages &amp; graphics device?</p>
  
  <pre><code>library(ggplot2); library(sf); 
  library(tmap); library(tidyverse)
  library(here) # project directory
  
  data(World) # sf object from tmap; Provides Africa polygon
  
  # 'd' data pulled from acleddata.com/data, restricted to Aug 18 2017  - Aug 18 2018, Region: N/S/E/W/Middle Africa only
  d &lt;- read.csv(here('2017-08-18-2018-08-18-Eastern_Africa-Middle_Africa-Northern_Africa-Southern_Africa-Western_Africa.csv'))
  dsf &lt;- st_as_sf(d, coords = c('longitude', 'latitude'), crs = 4326)
  </code></pre>
  
  <p>Data used: 1 -- 'World' shapefile data from the <code>tmap</code> package itself, and</p>
  
  <p>2 -- <a href="http://acleddata.com/data" rel="noreferrer">acleddata.com/data</a>, ACLED conflict events restricted to Africa between August 18 2017 and August 18 2018 (7.8 MB .csv; these filters:)</p>
  
  <p><a href="https://i.stack.imgur.com/iEA9j.png" rel="noreferrer"><img src="https://i.stack.imgur.com/iEA9j.png" alt="enter image description here"></a></p>
  
  <p>Plot rendering:</p>
  
  <pre><code># ggplot2; build plot, assign to object
  dev.cur()   # RStudioGD on macOS: quartz
  system.time(p &lt;- ggplot(World %&gt;% filter(continent == 'Africa')) + 
    geom_sf() + 
    geom_sf(data = dsf, aes(fill = event_type, 
                color = event_type)) + 
    ggthemes::theme_tufte() + 
    theme(legend.key.size = unit(.1, 'cm'),
          legend.title = element_blank()))
     # user  system elapsed 
     # 0.016   0.001   0.017 
  
  object.size(p)
  # 175312 bytes
  
  # render
  system.time(print(p))
  # user  system elapsed 
  # 84.755   0.432  85.418  # Note over 80 seconds
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/BCYzl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/BCYzl.png" alt="ggplot2 png"></a></p>
  
  <pre><code># tmap; build plot, assign to object
  tmap_mode('plot')
  system.time(tm &lt;- tm_shape(World, filter = 
                          (World$continent == 'Africa')) + 
                 tm_polygons(group = 'Countries') +
                 tm_shape(dsf) + 
                 tm_dots(col = 'event_type', group = 'event_type'))
      # user  system elapsed 
      # 0.000   0.000   0.001 
  
  object.size(tm)
  # 14331968 bytes   # This is 80x ggplot2 plot's object size
                     # 14331968/175312 = 81.75121
  # render
  dev.cur()    # RStudioGD on macOS: quartz
  system.time(print(tm))
  # user  system elapsed 
  # 1.438   0.038   1.484   # Note 1 second
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/5lgZB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5lgZB.png" alt="tmap png"></a></p>
  
  <hr>
  
  <p>[Previous inquiry into geom_sf() &amp; graphics devices, without the tmap comparison:]</p>
  
  <p>TL;DR:</p>
  
  <p>I am trying to speed up my plotting speed by switching graphics devices to <em>X11</em>, since my default <em>Quartz</em> graphics device is slow. After downloading XQuartz (to connect to the X11 graphics device) and calling <code>grDevices::X11()</code>, I don't understand the errors I'm getting.</p>
  
  <pre><code>X11(type = "cairo")
  # Error in .External2(C_X11, d$display, d$width, d$height, d$pointsize,  : 
                      #   unable to start device X11
                      # In addition: Warning message:
                      #   In X11() : unable to open connection to X11 display 'cairo'
  #&gt; Warning in X11(type = "cairo"): unable to open connection to X11 display ''
  </code></pre>
  
  <p>And when I call R from a XQuartz.app terminal on macOS instead, the error message is slightly different:</p>
  
  <pre><code>X11(type = "cairo")
  #&gt; Error in .External2(C_X11, d$display, d$width, d$height, d$pointsize, : unable to start device X11cairo
  </code></pre>
  
  <p>End TL;DR</p>
  
  <p>~~~~~~~</p>
  
  <p>Broader Context:</p>
  
  <p>Plotting large shapefiles with <code>ggplot2::geom_sf()</code>, the quartz graphics device used in macOS plots considerably slower than other devices, and while this larger performance issue is being resolved, I want to change my device from Quartz to X11.</p>
  
  <p>I downloaded XQuartz, following advice from the <a href="https://community.rstudio.com/t/ggplot2-geom-sf-performance/3251" rel="noreferrer">RStudio forums</a>, but my code doesn't successfully call X11, even when I launch R from XQuartz.</p>
  
  <p>Proof, using the same data as the RStudio forum poster:</p>
  
  <pre><code>library(sf)
  #&gt; Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
  library(ggplot2)
  tmpzip &lt;- tempfile(fileext = ".zip")
  download.file("https://github.com/bcgov/bcmaps.rdata/blob/master/data-raw/ecoregions/ecoregions.zip?raw=true", destfile = tmpzip)
  gdb_path &lt;- unzip(tmpzip, exdir = tempdir())
  ecoregions &lt;- sf::read_sf(dirname(gdb_path[1]))
  
  ## Create ggplot2 object
  ecoregions_gg &lt;- ggplot() + geom_sf(data = ecoregions)
  
  # Running quartz device - default macOS
  system.time(print(ecoregions_gg))
  #&gt;    user  system elapsed 
  #&gt; 128.980   0.774 130.375
  
  ### ^ Note two full minutes!
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ILhQP.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ILhQP.png" alt="Shapefile"></a></p>
  
  <p>This default device runs for an unusually long 129 seconds given the size.
  X11 should run faster according to the RStudio forum. A test on a (granted, faster) Windows 7 machine (32 GB RAM, 3.60 GHz) using its default graphics device (not Quartz), yielded:</p>
  
  <pre><code>#&gt;    user  system elapsed 
  #&gt;    2.16    2.24    4.46
  
  ###  ^Only two seconds
  </code></pre>
  
  <p>While people are troubleshooting the general geom_sf / Quartz performance problems (<a href="https://github.com/tidyverse/ggplot2/issues/2655" rel="noreferrer">Github Issue 1</a>, <a href="https://github.com/tidyverse/ggplot2/issues/2718" rel="noreferrer">Github Issue 2</a>), how can I use my XQuartz install to run X11 and speed up my shapefile plotting?</p>