These slides are viewed best by Chrome or Firefox and occasionally need to be refreshed if elements did not load properly. See here for the PDF .
Press the right arrow to progress to the next slide!
Instructor: Emi Tanaka
emi.tanaka@monash.edu
8th Dec 2021 @ Statistical Society of Australia Canberra Branch | Zoom
Murrell (2019) R Graphics. CRC Press.
ggplot
graphicsgraphics
library(graphics)
(typically already loaded)
graphics
library(graphics)
(typically already loaded)
grid
graphicslibrary(grid)
graphics
library(graphics)
(typically already loaded)
grid
graphicslibrary(grid)
graphics
library(graphics)
(typically already loaded)
grid
graphicslibrary(grid)
grid
grid
R-packagegrid
package contains the low-level functions to create, manipulate and grid
R-packageThe grid
package contains the low-level functions to create, manipulate and
draw graphical objects (grob).
The grid
package is a base package and so is included in standard R installations
packageDescription("grid")
Package: gridVersion: 4.1.2Priority: baseTitle: The Grid Graphics PackageAuthor: Paul Murrell <paul@stat.auckland.ac.nz>Maintainer: R Core Team <do-use-Contact-address@r-project.org>Contact: R-help mailing list <r-help@r-project.org>Description: A rewrite of the graphics layout capabilities, plus some support for interaction.Imports: grDevices, utilsLicense: Part of R 4.1.2NeedsCompilation: yesBuilt: R 4.1.2; x86_64-apple-darwin17.0; 2021-11-01 20:58:55 UTC; unix-- File: /Library/Frameworks/R.framework/Versions/4.1/Resources/library/grid/Meta/package.rds
stats
, utils
, datasets
, methods
, base
, graphics
and grDevices
are automatically loaded in a standard launch of R.library(grid)
stats
, utils
, datasets
, methods
, base
, graphics
and grDevices
are automatically loaded in a standard launch of R.library(grid)
grid.circle()
stats
, utils
, datasets
, methods
, base
, graphics
and grDevices
are automatically loaded in a standard launch of R.library(grid)
grid.circle()
circleGrob()
## circle[GRID.circle.94]
stats
, utils
, datasets
, methods
, base
, graphics
and grDevices
are automatically loaded in a standard launch of R.library(grid)
grid.circle()
circleGrob()
## circle[GRID.circle.94]
grid.draw(circleGrob())
grid
grid.lines()
linesGrob()
grid.polyline()
polylineGrob()
grid.segments()
segmentsGrob()
grid.xspline()
xsplineGrob()
grid.bezier()
bezierGrob()
grid.text()
textGrob()
grid.raster()
rasterGrob()
grid.circle()
circleGrob()
grid.polygon()
polygonGrob()
grid.path()
pathGrob()
grid.curve()
curveGrob()
grid.points()
pointsGrob()
grid.rect()
rectGrob()
grid.roundrect()
roundrectGrob()
Convention of function names:
grid.
primitive
()
primitive
Grob()
grid
are low-level functions but selected number of higher-level functions existpushViewport(plotViewport())pushViewport(viewport(xscale = c(0, 100)))grid.xaxis()
shape <- circleGrob(x = 0.5, y = 0.5, r = 0.5, name = "shape")eyes <- circleGrob(x = c(0.25, 0.75), y = 0.5, r = 0.05, gp = gpar(fill = "black"), name = "eyes")mouth <- circleGrob(x = 0.5, y = 0.2, r = 0.1, name = "mouth")
shape <- circleGrob(x = 0.5, y = 0.5, r = 0.5, name = "shape")eyes <- circleGrob(x = c(0.25, 0.75), y = 0.5, r = 0.05, gp = gpar(fill = "black"), name = "eyes")mouth <- circleGrob(x = 0.5, y = 0.2, r = 0.1, name = "mouth")
face1 <- gList(shape, eyes, mouth)face2 <- grobTree(shape, eyes, mouth, name = "face")
shape <- circleGrob(x = 0.5, y = 0.5, r = 0.5, name = "shape")eyes <- circleGrob(x = c(0.25, 0.75), y = 0.5, r = 0.05, gp = gpar(fill = "black"), name = "eyes")mouth <- circleGrob(x = 0.5, y = 0.2, r = 0.1, name = "mouth")
face1 <- gList(shape, eyes, mouth)face2 <- grobTree(shape, eyes, mouth, name = "face")
grid.draw(face1)
grid.draw(face2)
grid.ls(face1)
## shape## eyes## mouth
grid.ls(face2)
## face## shape## eyes## mouth
shape <- circleGrob(x = 0.5, y = 0.5, r = 0.5, name = "shape")eyes <- circleGrob(x = c(0.25, 0.75), y = 0.5, r = 0.05, gp = gpar(fill = "black"), name = "eyes")mouth <- circleGrob(x = 0.5, y = 0.2, r = 0.1, name = "mouth")
face1 <- gList(shape, eyes, mouth)face2 <- grobTree(shape, eyes, mouth, name = "face")
grid.draw(face1)
grid.draw(face2)
grid.ls(face1)
## shape## eyes## mouth
grid.ls(face2)
## face## shape## eyes## mouth
class(face1)
## [1] "gList"
class(face2)
## [1] "gTree" "grob" "gDesc"
g1 <- grob(x = unit(0.5, "npc"), y = unit(0.5, "npc"), r = unit(0.5, "npc"), cl = "circle")grid.draw(g1)
g1 <- grob(x = unit(0.5, "npc"), y = unit(0.5, "npc"), r = unit(0.5, "npc"), cl = "circle")grid.draw(g1)
g2 <- gTree(children = gList(shape, eyes, mouth))grid.draw(g2)
g1 <- grob(x = unit(0.5, "npc"), y = unit(0.5, "npc"), r = unit(0.5, "npc"), cl = "circle")grid.draw(g1)
g2 <- gTree(children = gList(shape, eyes, mouth))grid.draw(g2)
grobTree(x)
is essentially gTree(children = x)
Work with output | Work with grobs | Description |
---|---|---|
grid.get() |
getGrob() |
Return a copy of grobs |
grid.edit() |
editGrob() |
Modifies grobs |
grid.add() |
addGrob() |
Add a grob |
grid.remove() |
removeGrob() |
Remove grobs |
grid.set() |
setGrob() |
Replace grobs |
Along with some other helpful functions like :
grid.newpage()
to erase current device or move to a new pagegrid.grep()
to find all matching grobs grid.ls(face2)
## face## shape## eyes## mouth
grid.draw(removeGrob(face2, "mouth"))
grid.ls(face2)
## face## shape## eyes## mouth
grid.draw(removeGrob(face2, "mouth"))
xaxis <- xaxisGrob()
grid.ls(face2)
## face## shape## eyes## mouth
grid.draw(removeGrob(face2, "mouth"))
xaxis <- xaxisGrob()
grid.ls(xaxis)
## GRID.xaxis.99
grid.ls(face2)
## face## shape## eyes## mouth
grid.draw(removeGrob(face2, "mouth"))
xaxis <- xaxisGrob()
grid.ls(xaxis)
## GRID.xaxis.99
xaxis
contains no children grid.ls(face2)
## face## shape## eyes## mouth
grid.draw(removeGrob(face2, "mouth"))
xaxis <- xaxisGrob()
grid.ls(xaxis)
## GRID.xaxis.99
xaxis
contains no children xaxisf <- grid.force(xaxis)grid.ls(xaxisf)
## GRID.xaxis.99## major## ticks## labels
grid
col
color
fill
fill
alpha
opacity
lwd
line width
lex
line width expansion
lty
line type
lineend
line end style
linejoin
line join style
linemitre
line mitre limit
All graphical primitives have a gp
argument to parse with grid::gpar()
cex
character expansion
fontsize
font size
fontface
font face
fontfamily
font family
lineheight
line height
Coordinate system | Description |
---|---|
"native" |
Relative to the scales of the current viewport |
"npc" |
Normalised parent coordinates |
"snpc" |
Square normalised parent coordinates |
"in" , "cm" , "mm" |
Physical inches, centimeters, millimeters |
"pt" , "bigpts" , "picas" ,"dida" , "cicero" , "scaledpts" |
72.27 points = 1 inch, 72 big points = 1 inch, 12 points = 1 pica, 1157 dida = 1248 points, 1 cicero = 12 dida, 65536 scaled points = 1 point |
"char" |
Multiples of the current font size (fontsize and cex ) |
"line" |
Multiples of the height of a text line (fontsize , cex and lineheight ) |
"strwidth" , "strheight" |
Multiples of the width/height of a given string (fontsize , cex , fontfamily , and fontface ) |
"grobx" , "groby" |
Multiples of the x- and y-location on the boundary of a given grob |
"grobwidth" , "grobheight" |
Multiples of the width/height of a given grob |
grid
1grid.rect(x = 0, y = 0, width = 0.25, height = 0.25, default.units = "npc", just = c("left", "bottom"), gp = gpar(fill = "pink"), vp = NULL)
grid
2grid.rect(x = 0.5, y = 0.5, width = 0.25, height = 0.25, default.units = "npc", just = c("left", "bottom"), gp = gpar(fill = "pink"), vp = NULL)
grid
3grid.rect(x = 0.5, y = 0.5, width = 0.25, height = 0.25, default.units = "npc", just = c("center", "center"), gp = gpar(fill = "pink"), vp = NULL)
grid
4grid.rect(x = 0.5, y = 0.5, width = 0.5, height = 0.5, default.units = "npc", just = c("center", "center"), gp = gpar(fill = "pink"), vp = NULL)
grid
5grid.rect(x = 0.5, y = 0.5, width = 0.5, height = 0.5, default.units = "in", just = c("left", "bottom"), gp = gpar(fill = "pink"), vp = NULL)
This canvas is 5 inches high and 7 inches wide
grid
6grid.rect(x = unit(0.5, "npc"), y = unit(0.5, "npc"), width = 0.5, height = 0.5, default.units = "in", just = c("left", "bottom"), gp = gpar(fill = "pink"), vp = NULL)
grid
7grid.circle( x = 1:6, y = 3, r = 1:6/6, default.units = "in", gp = gpar(fill = sample(colors(), 6)), vp = NULL)
vp = NULL
, this refers to the root viewport, i.e. the canvasgrid
1grid.circle(x = c(0.1, 0.9), y = c(0.1, 0.9), r = 0.2, default.units = "npc", gp = gpar(fill = "pink"), vp = NULL)
Graphical output outside of the root Viewport are not visible.
grid
2vp1 <- viewport(x = 0.1, y = 0.1, width = 0.5, height = 0.5, just = c("left", "bottom"))grid.circle(x = c(0.1, 0.9), y = c(0.1, 0.9), r = 0.2, default.units = "npc", gp = gpar(fill = "pink"), vp = vp1)
Graphical object is drawn relative to Viewport vp1
.
grid
2vp1 <- viewport(x = 0.1, y = 0.1, width = 0.5, height = 0.5, just = c("left", "bottom"))grid.circle(x = c(0.1, 0.9), y = c(0.1, 0.9), r = 0.2, default.units = "npc", gp = gpar(fill = "pink"), vp = vp1)
Graphical object is drawn relative to Viewport vp1
.
grid
3vp2 <- viewport(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", angle = 30) grid.rect(vp = vp2)
grid
3vp2 <- viewport(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", angle = 30) grid.rect(vp = vp2)pushViewport(vp2)
pushViewport(vp)
changes all subsequent drawing context to the viewport
object vp
grid
3vp2 <- viewport(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", angle = 30) grid.rect(vp = vp2)pushViewport(vp2)grid.rect(vp = vp2)
pushViewport(vp)
changes all subsequent drawing context to the viewport
object vp
grid
3vp2 <- viewport(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", angle = 30) grid.rect(vp = vp2)pushViewport(vp2)grid.rect(vp = vp2)pushViewport(vp2)
pushViewport(vp)
changes all subsequent drawing context to the viewport
object vp
grid
3vp2 <- viewport(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", angle = 30) grid.rect(vp = vp2)pushViewport(vp2)grid.rect(vp = vp2)pushViewport(vp2)grid.rect(vp = vp2)
pushViewport(vp)
changes all subsequent drawing context to the viewport
object vp
grid
3vp2 <- viewport(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", angle = 30) grid.rect(vp = vp2)pushViewport(vp2)grid.rect(vp = vp2)pushViewport(vp2)grid.rect(vp = vp2)popViewport()
pushViewport(vp)
changes all subsequent drawing context to the viewport
object vp
popViewport()
removes current viewport and reverts to the previous drawing contextgrid
3vp2 <- viewport(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", angle = 30, name = "vp-2") grid.rect(vp = vp2)pushViewport(vp2)grid.rect(vp = vp2)pushViewport(vp2)grid.rect(vp = vp2)popViewport()grid.text("Bottom-left corner", x = 0.025, y = 0.025, just = c("left", "bottom"))
pushViewport(vp)
changes all subsequent drawing context to the viewport
object vp
popViewport()
removes current viewport and reverts to the previous drawing contextgrid
4vp3 <- viewport(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", angle = 30, name = "vp-3") grid.rect(vp = vp2)pushViewport(vp2) grid.rect(vp = vp2)pushViewport(vp3)grid.rect(vp = vp2)upViewport(n = 2)grid.text("Bottom-left corner", x = 0.025, y = 0.025, just = c("left", "bottom"))downViewport("vp-3")grid.text("Bottom-left corner", x = 0.025, y = 0.025, just = c("left", "bottom"))
upViewport()
downViewport()
See Murrell (2019) "R Graphics" book for more about grid
day1-exercise-01.Rmd
15:00
devtools::session_info()
## ─ Session info 🤗 💤 🇲🇩 ─────────────────────────────────────────────────## hash: smiling face with open hands, zzz, flag: Moldova## ## setting value## version R version 4.1.2 (2021-11-01)## os macOS Catalina 10.15.7## system x86_64, darwin17.0## ui X11## language (EN)## collate en_AU.UTF-8## ctype en_AU.UTF-8## tz Australia/Melbourne## date 2021-12-07## pandoc 2.14.0.3 @ /Applications/RStudio.app/Contents/MacOS/pandoc/ (via rmarkdown)## ## ─ Packages ───────────────────────────────────────────────────────────────────## package * version date (UTC) lib source## bslib 0.3.1 2021-10-06 [1] CRAN (R 4.1.0)## cachem 1.0.6 2021-08-19 [1] CRAN (R 4.1.0)## callr 3.7.0 2021-04-20 [1] CRAN (R 4.1.0)## cli 3.1.0 2021-10-27 [1] CRAN (R 4.1.0)## colorspace 2.0-2 2021-06-24 [1] CRAN (R 4.1.0)## countdown 0.3.5 2021-12-07 [1] Github (gadenbuie/countdown@a544fa4)## crayon 1.4.2 2021-10-29 [1] CRAN (R 4.1.0)## desc 1.4.0 2021-09-28 [1] CRAN (R 4.1.0)## devtools 2.4.2 2021-06-07 [1] CRAN (R 4.1.0)## digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.0)## ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0)## evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.0)## fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.0)## fs 1.5.0 2020-07-31 [1] CRAN (R 4.1.0)## glue 1.5.0 2021-11-07 [1] CRAN (R 4.1.0)## highr 0.9 2021-04-16 [1] CRAN (R 4.1.0)## htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.0)## httr 1.4.2 2020-07-20 [1] CRAN (R 4.1.0)## jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.1.0)## jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.1.0)## kableExtra 1.3.4 2021-02-20 [1] CRAN (R 4.1.0)## knitr 1.36 2021-09-29 [1] CRAN (R 4.1.0)## lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.0)## magrittr * 2.0.1 2020-11-17 [1] CRAN (R 4.1.0)## memoise 2.0.0 2021-01-26 [1] CRAN (R 4.1.0)## munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.0)## pkgbuild 1.2.0 2020-12-15 [1] CRAN (R 4.1.0)## pkgload 1.2.3 2021-10-13 [1] CRAN (R 4.1.0)## prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.1.0)## processx 3.5.2 2021-04-30 [1] CRAN (R 4.1.0)## ps 1.6.0 2021-02-28 [1] CRAN (R 4.1.0)## purrr 0.3.4 2020-04-17 [1] CRAN (R 4.1.0)## R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.0)## remotes 2.4.1 2021-09-29 [1] CRAN (R 4.1.0)## rlang 0.4.12 2021-10-18 [1] CRAN (R 4.1.0)## rmarkdown 2.11 2021-09-14 [1] CRAN (R 4.1.0)## rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.1.0)## rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.0)## rvest 1.0.2 2021-10-16 [1] CRAN (R 4.1.0)## sass 0.4.0 2021-05-12 [1] CRAN (R 4.1.0)## scales 1.1.1 2020-05-11 [1] CRAN (R 4.1.0)## sessioninfo 1.2.1 2021-11-02 [1] CRAN (R 4.1.0)## stringi 1.7.5 2021-10-04 [1] CRAN (R 4.1.0)## stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.0)## svglite 2.0.0 2021-02-20 [1] CRAN (R 4.1.0)## systemfonts 1.0.3 2021-10-13 [1] CRAN (R 4.1.2)## testthat 3.1.0 2021-10-04 [1] CRAN (R 4.1.0)## usethis 2.1.3 2021-10-27 [1] CRAN (R 4.1.0)## viridisLite 0.4.0 2021-04-13 [1] CRAN (R 4.1.0)## webshot 0.5.2 2019-11-22 [1] CRAN (R 4.1.0)## whisker 0.4 2019-08-28 [1] CRAN (R 4.1.0)## withr 2.4.2 2021-04-18 [1] CRAN (R 4.1.0)## xaringan 0.22 2021-06-23 [1] CRAN (R 4.1.0)## xfun 0.28 2021-11-04 [1] CRAN (R 4.1.0)## xml2 1.3.2 2020-04-23 [1] CRAN (R 4.1.0)## yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.0)## ## [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library## ## ──────────────────────────────────────────────────────────────────────────────
These slides are licensed under
Instructor: Emi Tanaka
emi.tanaka@monash.edu
8th Dec 2021 @ Statistical Society of Australia Canberra Branch | Zoom
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |