Display Lists in grid

Similar documents
Display Lists in grid

Modifying grid grobs

A GUI-Builder Approach to grid Graphics

Talk Overview. grid Graphics and Programming. The Structure of R Graphics. Introduction to grid

Writing grid Code. Paul Murrell. December 14, 2009

Package gridgraphics

R Graphics. Paul Murrell. The University of Auckland. R Graphics p.1/47

The LDheatmap Package

The gridsvg package. Paul Murrell. October 30, 2017

Types of Plotting Functions. Managing graphics devices. Further High-level Plotting Functions. The plot() Function

DATA VISUALIZATION WITH GGPLOT2. Grid Graphics

Package grimport2. March 20, 2018

Introduction to R 21/11/2016

Package gridextra. September 9, 2017

Working with grid Viewports

Plotting: An Iterative Process

Overview: Printing MFworks Documents

CIND123 Module 6.2 Screen Capture

Working with grid Viewports

Package grimport. R topics documented: February 20, 2015

Configuring Figure Regions with prepplot Ulrike Grömping 03 April 2018

R Lattice Graphics. Paul Murrell

A Handbook of Statistical Analyses Using R. Brian S. Everitt and Torsten Hothorn

Package arphit. March 28, 2019

Animation with the gridsvg package

Advanced Econometric Methods EMET3011/8014

Graphics in R STAT 133. Gaston Sanchez. Department of Statistics, UC Berkeley

3D Modeler Creating Custom myhouse Symbols

12 Interacting with Trellis Displays

FILE MENU New Starts a New Blank Canvas Open- Allows you to import designs into the program. Close- Closes the current Design

A Step-by-step guide to creating a Professional PowerPoint Presentation

Web-Friendly Sites. Planning & Design 1

Intro to R Graphics Center for Social Science Computation and Research, 2010 Stephanie Lee, Dept of Sociology, University of Washington

Statistical Programming with R

Package Grid2Polygons

Graphics - Part III: Basic Graphics Continued

grid Graphics Paul Murrell December 7, 2017

Adobe Flash CS3 Reference Flash CS3 Application Window

The nor1mix Package. August 3, 2006

Applied Calculus. Lab 1: An Introduction to R

2IS45 Programming

2 A little on Spreadsheets

c122sep2214.notebook September 22, 2014

Microsoft Excel 2000 Charts

Examples for ltxsparklines package

Working with Tables in Microsoft Word

Microsoft Excel 2002 M O D U L E 2

Geometry: Semester 2 Practice Final Unofficial Worked Out Solutions by Earl Whitney

The nor1mix Package. June 12, 2007

Analysing Spatial Data in R: Vizualising Spatial Data

Package sciplot. February 15, 2013

Sorting Fields Changing the Values Line Charts Scatter Graphs Charts Showing Frequency Pie Charts Bar Charts...

NUMERICAL COMPUTING For Finance Using Excel. Sorting and Displaying Data

Web Design and Implementation

WORD Creating Objects: Tables, Charts and More

Introduction to R. Biostatistics 615/815 Lecture 23

Package pathdiagram. R topics documented: August 29, 2016

grid Graphics Paul Murrell October 6, 2005

ADJUST TABLE CELLS-ADJUST COLUMN AND ROW WIDTHS

R Package shape: functions for plotting graphical shapes, colors...

GCSE CCEA GCSE EXCEL 2010 USER GUIDE. Business and Communication Systems

Excel Core Certification

Plotting Complex Figures Using R. Simon Andrews v

Tutorial - FactoryPLAN

Microsoft Office Word 2010

1. Study the image below and table on the next page. Complete the following tasks.

42 Editing MSC Diagrams

The MathWorks - MATLAB Digest June Exporting Figures for Publication

Layout Tutorial. Getting Started. Creating a Layout Template

Trevor Darrell. PostScript gures are automatically scaled and positioned on the page, and the

Accessing Rhino Page Layouts

Package ggrepel. September 30, 2017

End-of-Module Assessment Task

R practice. Eric Gilleland. 20th May 2015

Statistical Software Camp: Introduction to R

Organizing and Summarizing Data

Renko Bar Indicator. 1. Overview Using the Renko indicator... 3

AA BB CC DD EE. Introduction to Graphics in R

but both the "what" and the "how" can get quite complicated.

Class #2 Lab: Basic CAD Skills & Standards. Basic AutoCAD Interface AutoCAD Skills AutoCAD Standards

FUNDAMENTAL SPREADSHEET APPLICATIONS (230)

Microsoft Excel 2010 Tutorial

Chapter 2 Surfer Tutorial

Statistical Programming Camp: An Introduction to R

Controlling the Drawing Display

Exploratory Data Analysis - Part 2 September 8, 2005

Tutorial 2: Terrain and Dynamic City Layouts

Text. Text metrics. There are some important metrics that we must consider when working with text. Figure 4-1 shows the basics.

Matrix algebra. Basics

DSCI 325: Handout 18 Introduction to Graphics in R

3D Body. Modified by Jason Howie on 8-Oct Parent page: PCB Dialogs. Other Related Resources 3D Body (Object)

Creating a Basic Chart in Excel 2007

Graphing Single Subject Research Data. AAC Colloquium October 18 th, 2017

Package gridgraphviz

Creating Two-dimensional Arrays

Microsoft Excel training course

Animating Objects in Microsoft PowerPoint 2003

The figures below are all prisms. The bases of these prisms are shaded, and the height (altitude) of each prism marked by a dashed line:

UNIVERSITY OF CAMBRIDGE INTERNATIONAL EXAMINATIONS International General Certificate of Secondary Education

Excel 2013 Intermediate

Transcription:

Display Lists in grid Paul Murrell April 13, 2004 A display list is a record of drawing operations. It is used to redraw graphics output when a graphics window is resized, when graphics output is copied from one device to another, and when graphics output is edited (via grid.edit). There are two display lists that can be used when working with grid. R s graphics engine maintains a display list and grid maintains its own display list. The former is maintained at the C code level and records both base graphics output and grid graphics output. The latter is maintained at the R code level and only records grid output. In standard usage, the graphics engine s display list is used to redraw when a window is resized and when copying between devices. grid s display list is used for redrawing when editing grid output. There are two main problems with this standard usage: 1. The graphics engine display list only records graphics output; none of the calculations leading up to producing the output are recorded. This particularly impacts on plots which perform calculations based on the physical dimensions of the device an example is the legend function which performs calculations in order to arrange the elements of the legend. The effect can be seen from any example which uses the legend function. Try running example(legend) then resize the device (make it quite tall and thin or quite wide and fat); the legend will start to look pretty sick. NOTE: that this is a problem with the graphics engine display list it is not specific to grid. In fact, much of grid s behaviour is protected from this problem because things like grid units are declarative and will be reevaluated on each redraw. However, there are situations where grid output can be afflicted, in particular, whenever the convertunit() function (or one of its variants) is used (the help file for convertunit() gives an example). A situation where this problem becomes very relevant for grid output is when the gridbase package is used. This is a situation where lots of calculations are performed in order to align base and grid output, but these calculations are not recorded on the graphics engine display list, so if the device is resized the output will become very yukky. 2. grid s display list does not record base graphics output 1 so if both base and grid 1 This is not quite true; it is possible to include base graphics output on the grid display list as we will see later. 1

output appear on the same device then the result of editing will not redraw the base output. The following code provides a simple example: > plot(1:10) > par(new = TRUE) > grid.rect(width = 0.5, height = 0.5, gp = gpar(lwd = 3), + name = "gr") 1:10 2 4 6 8 2 4 6 8 10 Index > grid.edit("gr", gp = gpar(col = "red", lwd = 3)) After the grid.edit, the rectangle has been redrawn, but the base plot has not. 2

Avoiding the graphics engine display list The problems described above can all be avoided if grid is able to control all redrawing of output. There are several parts to this solution: ˆ It must be possible to avoid using the graphics engine display list. This facility is now provided by the engine.display.list() function. The following code will stop grid output from being recorded on the graphics engine display list: > engine.display.list(false) ˆ grid must be alerted whenever a device has been resized or output is being copied between devices. This is handled in C code; if the graphics engine display list is not being used then grid automatically redraws its own display list whenever a resize or copy occurs. ˆ Calculations must be recorded on grid s display list. If graphics output is based on calculations which will not be consistent when the device is resized (e.g., a call to convertunit(), or calls to the gridbase functions), then a device resize will produce inconsistent output unless the calculations are recorded somehow on grid s display list. This appears to be exactly the same problem as we had with the graphics engine display list; the difference is that there is a way to record calculations on grid s display list. When a grid grob is drawn the drawdetails method for that grob will be called; if calculations are put within a drawdetails method, then the calculations will be performed every time the grob is drawn. All of this means that it is possible, for example, to use convertunit() and have the result consistent across device resizese or copies 2. This next piece of code is an example where the output becomes inconsistent when the device is resized. We specify a width for the rectangle in inches, but convert it (gratuitously) to NPC coordinates when the device is resized, the NPC coordinates will no longer correspond to 1. > grid.rect(width = convertwidth(unit(1, "inches"), + "npc")) This second piece of code demonstrates that it is not enough simply to use grid s display list; the calculations are still not being recorded so the inconsistency will still occur. > engine.display.list(false) > grid.rect(width = convertwidth(unit(1, "inches"), + "npc")) Finally, the next piece of code demonstrates that, if we only use grid s display list and we place the calculations within a drawdetails method, then the output remains consistent across device resizes and copies 3. 2 In each of the examples that follow, you should execute the example code, resize the device to see any inconsistency, then close the device before trying the next example. 3 In order to avoid drawing the rectangle multiple times, we have draw the rectangle but not record it on the display list (recording=false; only the "myrect" grob is recorded on the display list). 3

> engine.display.list(false) > drawdetails.myrect <- function(x, x.wrapped, recording) { + gr <- rectgrob(width = convertwidth(unit(1, + "inches"), "npc")) + grid.draw(gr, recording = FALSE) > grid.draw(grob(cl = "myrect")) A more impressive use of this facility can be obtained from an example using gridbase to combine base and grid graphics output. Here I replicate the last example from the gridbase vignette a set of base pie charts within grid viewports within a base plot. In this case, I can produce all of the grobs required in the normal manner their locations and sizes are not based on special calculations 4. > if ("gridbase" %in%.packages(all.available = TRUE)) { + library(gridbase) + x <- c(0.88, 1, 0.67, 0.34) + y <- c(0.87, 0.43, 0.04, 0.94) + z <- matrix(runif(4 * 2), ncol = 2) + maxpiesize <- unit(1, "inches") + totals <- apply(z, 1, sum) + sizemult <- totals/max(totals) + gs <- segmentsgrob(x0 = unit(c(rep(0, 4), + x), rep(c("npc", "native"), each = 4)), + x1 = unit(c(x, x), rep("native", 8)), + y0 = unit(c(y, rep(0, 4)), rep(c("native", + "npc"), each = 4)), y1 = unit(c(y, + y), rep("native", 8)), gp = gpar(lty = "dashed", + col = "grey")) + gr <- rectgrob(gp = gpar(col = "grey", fill = "white", + lty = "dashed")) What is important is that I place the calls to the gridbase functions within the drawdetails method so that they are performed every time the grob is drawn and the calls to the base graphics functions are in here too so that they are called for every redraw. > drawdetails.pieplot <- function(grob, grob.wrapped, + recording) { + plot(x, y, xlim = c(-0.2, 1.2), ylim = c(-0.2, + 1.2), type = "n") + vps <- baseviewports() + par(new = TRUE) + pushviewport(vps$inner, vps$figure, vps$plot, + recording = FALSE) + grid.draw(grob$gs, recording = FALSE) 4 The example is wrapped inside a check for whether the gridbase package is installed so that the code will still run on systems without gridbase. 4

+ for (i in 1:4) { + pushviewport(viewport(x = unit(x[i], "native"), + y = unit(y[i], "native"), width = sizemult[i] * + maxpiesize, height = sizemult[i] * + maxpiesize), recording = FALSE) + grid.draw(grob$gr, recording = FALSE) + par(plt = gridplt(), new = TRUE) + pie(z[i, ], radius = 1, labels = rep("", + 2)) + popviewport(recording = FALSE) + popviewport(3, recording = FALSE) The pieplot is created by assembling the component grobs into a collective grob of the appropriate class; the drawdetails method takes care of actually producing the output. An important additional step here is that I turn off the graphics engine display list for the base graphics also (otherwise I would get multiple copies of the base graphics output). > if ("gridbase" %in%.packages()) { + engine.display.list(false) + dev.control(displaylist = "inhibit") + grid.draw(grob(gs = gs, gr = gr, cl = "pieplot")) The output from this example can be resized safely; grid handles all of the redrawing, and performs all of the actions within the drawdetails method for each redraw, including redrawing the base graphics output! As a final example, we will harness the grid display list purely to achieve consistency in base graphics output. The following reproduces the last example from the legend() help page, but produces output which can be resized without the legend going crazy. > drawdetails.mylegend <- function(grob, grob.wrapped, + recording) { + x <- 0:64/64 + y <- sin(3 * pi * x) + plot(x, y, type = "l", col = "blue", main = "points with bg & legend(*, pt.bg)") + points(x, y, pch = 21, bg = "white") + legend(0.4, 1, "sin(c x)", pch = 21, pt.bg = "white", + lty = 1, col = "blue") > engine.display.list(false) > dev.control(displaylist = "inhibit") > grid.draw(grob(cl = "mylegend")) 5

Disadvantages to using grid for all redraws The main problem here is that redrawing grid s display list is slower than redrawing the graphics engine display list. This is just the price that must be paid in order to obtain the advantages of using grid s display list. 6