Grid Graphics Data Visualization with ggplot2 ggplot2 internals 35 - - PowerPoint PPT Presentation

grid graphics
SMART_READER_LITE
LIVE PREVIEW

Grid Graphics Data Visualization with ggplot2 ggplot2 internals 35 - - PowerPoint PPT Presentation

DATA VISUALIZATION WITH GGPLOT2 Grid Graphics Data Visualization with ggplot2 ggplot2 internals 35 Explore grid graphics 30 Elements of ggplot2 plot 25 mpg How do graphics work in R? 20 2 plo ing systems 15 10


slide-1
SLIDE 1

DATA VISUALIZATION WITH GGPLOT2

Grid Graphics

slide-2
SLIDE 2

Data Visualization with ggplot2

ggplot2 internals

  • Explore grid graphics
  • Elements of ggplot2 plot
  • How do graphics work in R?
  • 2 ploing systems
  • base package
  • grid graphics

10 15 20 25 30 35 2 3 4 5

wt mpg

slide-3
SLIDE 3

Data Visualization with ggplot2

base package

> plot(mtcars$wt, mtcars$mpg, pch = 16, col = "#00000080")

2 3 4 5 10 15 20 25 30 mtcars$wt mtcars$mpg

slide-4
SLIDE 4

Data Visualization with ggplot2

base package

> plot(mtcars$wt, mtcars$mpg, pch = 16, col = "#00000080") > abline(lm(mpg ~ wt, data = mtcars), col = "red")

2 3 4 5 10 15 20 25 30 mtcars$wt mtcars$mpg

slide-5
SLIDE 5

Data Visualization with ggplot2

base package - change labels

2 3 4 5 10 15 20 25 30 mtcars$wt mtcars$mpg Weight mpg (US gallons)

> plot(mtcars$wt, mtcars$mpg, pch = 16, col = "#00000080") > abline(lm(mpg ~ wt, data = mtcars), col = "red") > mtext("Weight", 1, 3) > mtext("mpg (US gallons)", 2, 3)

slide-6
SLIDE 6

Data Visualization with ggplot2

base package - change dots

2 3 4 5 10 15 20 25 30 mtcars$wt mtcars$mpg

> plot(mtcars$wt, mtcars$mpg, pch = 16, col = "#00000080") > abline(lm(mpg ~ wt, data = mtcars), col = "red") > points(mtcars$wt, mtcars$mpg, pch = 16, 
 col = "#C3212766", cex = 2)

slide-7
SLIDE 7

Data Visualization with ggplot2

base package - restart

2 3 4 5 10 15 20 25 30 Weight mpg (US gallons)

> plot(mtcars$wt, mtcars$mpg, pch = 16, col = "#C3212766", cex = 2, xlab = "Weight", ylab = "mpg (US gallons)") > abline(lm(mpg ~ wt, data = mtcars), col = "red")

slide-8
SLIDE 8

Data Visualization with ggplot2

grid package

  • Paul Murell
  • Low-level graphic functions
  • Assemble yourself
  • ggplot2 built on top of grid
  • Two components
  • Create graphic outputs
  • Layer and position outputs with viewports
slide-9
SLIDE 9

Data Visualization with ggplot2

Graphic output

> # Rectangle > grid.rect()

slide-10
SLIDE 10

Data Visualization with ggplot2

Graphic output

> # Rectangle > grid.rect() > # Line > grid.lines()

slide-11
SLIDE 11

Data Visualization with ggplot2

Graphic output

> # Rectangle > grid.rect() > # Line > grid.lines() > # Circle > grid.circle()

slide-12
SLIDE 12

Data Visualization with ggplot2

Graphic output

> # Rectangle > grid.rect() > # Line > grid.lines() > # Circle > grid.circle() > # Grid polygon > grid.polygon()

slide-13
SLIDE 13

Data Visualization with ggplot2

Graphic output

> # Rectangle > grid.rect() > # Line > grid.lines() > # Circle > grid.circle() > # Grid polygon > grid.polygon() > # Text > grid.text("hello")

hello

slide-14
SLIDE 14

Data Visualization with ggplot2

Graphic output - adjust

> # Rectangle > grid.rect() > # Modified rectangle > grid.rect(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center")

slide-15
SLIDE 15

Data Visualization with ggplot2

Graphic output - gpar()

> # Rectangle > grid.rect(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", gp = gpar(fill = "darkgreen"))

slide-16
SLIDE 16

Data Visualization with ggplot2

Graphic output - gpar()

> # Rectangle > grid.rect(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", gp = gpar(fill = "darkgreen")) > # Line > grid.lines(x = c(0, 0.5), y = c(0.25, 1), gp = gpar(lty = 3, 
 col = "darkblue"))

slide-17
SLIDE 17

Data Visualization with ggplot2

Graphic output - gpar()

> # Rectangle > grid.rect(x = 0.5, y = 0.5, width = 0.5, height = 0.5, just = "center", gp = gpar(fill = "darkgreen")) > # Line > grid.lines(x = c(0, 0.5), y = c(0.25, 1), gp = gpar(lty = 3, 
 col = "darkblue")) > # Circle > grid.circle(x = 0.5, y = 0.5, r = 0.25, gp = gpar(fill = "darkred", col = NA))

slide-18
SLIDE 18

Data Visualization with ggplot2

Naming graphic output

> # Circle > grid.circle(x = 0.5, y = 0.5, r = 0.25, gp = gpar(fill = "darkred", col = NA), name = "myCircle")

slide-19
SLIDE 19

Data Visualization with ggplot2

Naming graphic output

> # Circle > grid.circle(x = 0.5, y = 0.5, r = 0.25, gp = gpar(fill = "darkred", col = NA), name = "myCircle") > grid.edit("myCircle", gp = gpar(fill = "pink"))

slide-20
SLIDE 20

Data Visualization with ggplot2

Naming graphic output

> # Circle > grid.circle(x = 0.5, y = 0.5, r = 0.25, gp = gpar(fill = "darkred", col = NA), name = "myCircle") > grid.edit("myCircle", gp = gpar(fill = "pink")) > grid.edit("myCircle", x = unit(0.6, "npc"), r = unit(0.6, "npc"))

slide-21
SLIDE 21

Data Visualization with ggplot2

Viewports

Windows onto which we draw graphic outputs

> grid.rect(gp = gpar(fill = "grey80")) > vp <- viewport(x = 0.5, y = 0.5, w = 0.5, h = 0.5, 
 just = "center") > pushViewport(vp)

slide-22
SLIDE 22

Data Visualization with ggplot2

Viewports

Windows onto which we draw graphic outputs

> grid.rect(gp = gpar(fill = "grey80")) > vp <- viewport(x = 0.5, y = 0.5, w = 0.5, h = 0.5, 
 just = "center") > pushViewport(vp) > grid.rect(gp = gpar(fill = "#2685A2"))

slide-23
SLIDE 23

Data Visualization with ggplot2

Viewports

Windows onto which we draw graphic outputs

> grid.rect(gp = gpar(fill = "grey80")) > vp <- viewport(x = 0.5, y = 0.5, w = 0.5, h = 0.5, 
 just = "center" ) > pushViewport(vp) > grid.rect(gp = gpar(fill = "#2685A2")) , name = "vp1"

slide-24
SLIDE 24

Data Visualization with ggplot2

plotViewport

> grid.rect(gp = gpar(fill = "grey80")) > mar <- c(5, 4, 2, 2) > vp_plot <- plotViewport(margins = mar, name = "vp2") > pushViewport(vp_plot) > grid.rect(gp = gpar(fill = "#2685A2"))

slide-25
SLIDE 25

Data Visualization with ggplot2

dataViewport

> grid.rect(gp = gpar(fill = "grey80")) > mar <- c(5, 4, 2, 2) > vp_plot <- plotViewport(margins = mar, name = "vp2") > pushViewport(vp_plot) > vp_data <- dataViewport(mtcars$wt, mtcars$mpg) > pushViewport(vp_data) > grid.rect(gp = gpar(fill = "grey70"))

slide-26
SLIDE 26

Data Visualization with ggplot2

dataViewport

> grid.rect(gp = gpar(fill = "grey80")) > mar <- c(5, 4, 2, 2) > vp_plot <- plotViewport(margins = mar, name = "vp2") > pushViewport(vp_plot) > vp_data <- dataViewport(mtcars$wt, mtcars$mpg) > pushViewport(vp_data) > grid.rect(gp = gpar(fill = "grey70")) > grid.xaxis() > grid.yaxis()

2 3 4 5 10 15 20 25 30 35

slide-27
SLIDE 27

Data Visualization with ggplot2

dataViewport

> grid.rect(gp = gpar(fill = "grey80")) > mar <- c(5, 4, 2, 2) > vp_plot <- plotViewport(margins = mar, name = "vp2") > pushViewport(vp_plot) > vp_data <- dataViewport(mtcars$wt, mtcars$mpg) > pushViewport(vp_data) > grid.rect(gp = gpar(fill = "grey70")) > grid.xaxis() > grid.yaxis() > grid.text("Weight", y = unit(-3, "lines")) > grid.text("MPG", x = unit(-3, "lines"), rot = 90)

2 3 4 5 10 15 20 25 30 35 Weight MPG

slide-28
SLIDE 28

Data Visualization with ggplot2

dataViewport

> grid.rect(gp = gpar(fill = "grey80")) > mar <- c(5, 4, 2, 2) > vp_plot <- plotViewport(margins = mar, name = "vp2") > pushViewport(vp_plot) > vp_data <- dataViewport(mtcars$wt, mtcars$mpg) > pushViewport(vp_data) > grid.rect(gp = gpar(fill = "grey70")) > grid.xaxis() > grid.yaxis() > grid.text("Weight", y = unit(-3, "lines")) > grid.text("MPG", x = unit(-3, "lines"), rot = 90) > grid.points(mtcars$wt, mtcars$mpg, pch = 16, gp = gpar(col = "#00000080"), name = "data")

2 3 4 5 10 15 20 25 30 35 Weight MPG

slide-29
SLIDE 29

Data Visualization with ggplot2

grid.edit

> grid.rect(gp = gpar(fill = "grey80")) ... > grid.points(mtcars$wt,mtcars$mpg, pch = 16, gp = gpar(col = "#00000080"), name = "datapoints") > grid.edit("datapoints", gp = gpar(col = "#C3212766", cex = 4))

2 3 4 5 10 15 20 25 30 35 Weight MPG

slide-30
SLIDE 30

DATA VISUALIZATION WITH GGPLOT2

Let’s practice!

slide-31
SLIDE 31

DATA VISUALIZATION WITH GGPLOT2

Grid graphics in ggplot2

slide-32
SLIDE 32

Data Visualization with ggplot2

Grobs

  • Graphical objects = grobs
  • ggplot2 object = collection of grobs

Graphic Output Graphics Object grid.rect() rectGrob() grid.lines() linesGrob() grid.circle() circleGrob() grid.polygon() polygonGrob() grid.text() textGrob()

slide-33
SLIDE 33

Data Visualization with ggplot2

ggplot2 example

> p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point(alpha = 0.3, size = 5, shape = 16) + geom_smooth(method = "lm", se = FALSE) + scale_y_continuous("Width", limits = c(2, 4.5), expand = c(0,0)) + scale_x_continuous("Length", limits = c(4, 8), expand = c(0,0)) + coord_equal() + ggtitle("Iris Sepals") + theme(rect = element_blank())

slide-34
SLIDE 34

Data Visualization with ggplot2

ggplot2 example

> p

2.0 2.5 3.0 3.5 4.0 4.5 4 5 6 7 8

Length Width Species

setosa versicolor virginica

Iris Sepals

slide-35
SLIDE 35

Data Visualization with ggplot2

Accessing grobs

> library(grid) > g <- ggplotGrob(p) > g TableGrob (6 x 6) "layout": 9 grobs z cells name grob 1 0 (1-6,1-6) background zeroGrob[plot.background..zeroGrob.23938] 2 3 (3-3,3-3) axis-l absoluteGrob[GRID.absoluteGrob.23907] 3 1 (4-4,3-3) spacer zeroGrob[NULL] 4 2 (3-3,4-4) panel gTree[GRID.gTree.23893] 5 4 (4-4,4-4) axis-b absoluteGrob[GRID.absoluteGrob.23900] 6 5 (5-5,4-4) xlab titleGrob[axis.title.x..titleGrob.23910] 7 6 (3-3,2-2) ylab titleGrob[axis.title.y..titleGrob.23913] 8 7 (3-3,5-5) guide-box gtable[guide-box] 9 8 (2-2,4-4) title titleGrob[plot.title..titleGrob.23937]

slide-36
SLIDE 36

Data Visualization with ggplot2

List of grobs

> g$grob [[1]] zeroGrob[plot.background..zeroGrob.24133] ... [[6]] titleGrob[axis.title.x..titleGrob.24105] [[7]] titleGrob[axis.title.y..titleGrob.24108] [[8]] TableGrob (3 x 3) "guide-box": 1 grobs z cells name grob 99_cf2b20daa6ef538a0def731fa7c3e7db 1 (2-2,2-2) guides gtable[layout] ...

slide-37
SLIDE 37

Data Visualization with ggplot2

Legend grob

> g$grob[[8]] TableGrob (3 x 3) "guide-box": 1 grobs z cells name grob 99_cf2b20daa6ef538a0def731fa7c3e7db 1 (2-2,2-2) guides gtable[layout]

Species

setosa versicolor virginica

> grid.draw(g$grob[[8]])

slide-38
SLIDE 38

Data Visualization with ggplot2

Structure of legend

> library(gtable) > gtable_show_layout(g$grob[[8]])

(1, 1) 0.2cm 0.2cm (1, 2) max(sum(0.15cm, 0.6096cm, 0.0762cm, 1.32833232985602cm, 0cm, 0.15cm)) (1, 3) 0.2cm 0.2cm (2, 1) sum(0.15cm, 0.277778049045139cm, 0.1524cm, 0.6096cm, 0.6096cm, 0.6096cm, 0.15cm) (2, 2) (2, 3) sum(0.15cm, 0.277778049045139cm, 0.1524cm, 0.6096cm, 0.6096cm, 0.6096cm, 0.15cm) (3, 1) 0.2cm 0.2cm (3, 2) max(sum(0.15cm, 0.6096cm, 0.0762cm, 1.32833232985602cm, 0cm, 0.15cm)) (3, 3) 0.2cm 0.2cm

slide-39
SLIDE 39

Data Visualization with ggplot2

Update legend

> my_text <- textGrob(label = "Anderson, 1936", gp = gpar(fontsize = 7, col = "gray25"))

(1, 1) 0.2cm 0.2cm (1, 2) max(sum(0.15cm, 0.6096cm, 0.0762cm, 1.32833232985602cm, 0cm, 0.15cm)) (1, 3) 0.2cm 0.2cm (2, 1) sum(0.15cm, 0.277778049045139cm, 0.1524cm, 0.6096cm, 0.6096cm, 0.6096cm, 0.15cm) (2, 2) (2, 3) sum(0.15cm, 0.277778049045139cm, 0.1524cm, 0.6096cm, 0.6096cm, 0.6096cm, 0.15cm) (3, 1) 0.2cm 0.2cm (3, 2) max(sum(0.15cm, 0.6096cm, 0.0762cm, 1.32833232985602cm, 0cm, 0.15cm)) (3, 3) 0.2cm 0.2cm

> leg2 <- gtable_add_grob(g$grob[[8]], my_text, 3, 2) > leg2 TableGrob (3 x 3) "guide-box": 2 grobs z cells name grob 99_cf2.... 1 (2-2,2-2) guides gtable[layout] 2 (3-3,2-2) guide-box text[GRID.text.24881]

slide-40
SLIDE 40

Data Visualization with ggplot2

Update legend

> my_text <- textGrob(label = "Anderson, 1936", gp = gpar(fontsize = 7, col = "gray25")) > leg2 <- gtable_add_grob(g$grob[[8]], my_text, 3, 2) > leg2 TableGrob (3 x 3) "guide-box": 2 grobs z cells name grob 99_cf2.... 1 (2-2,2-2) guides gtable[layout] 2 (3-3,2-2) guide-box text[GRID.text.24881]

Species

setosa versicolor virginica

Anderson, 1936

> grid.draw(leg2)

slide-41
SLIDE 41

Data Visualization with ggplot2

Update legend (2)

> my_text <- textGrob(label = "Anderson, 1936", gp = gpar(fontsize = 7, col = "gray25")) > <- gtable_add_grob(g$grob[[8]], my_text, 3, 2) g$grobs[[8]] > grid.draw(g)

2.0 2.5 3.0 3.5 4.0 4.5 4 5 6 7 8

Length Width Species

setosa versicolor virginica

Anderson, 1936

Iris Sepals

slide-42
SLIDE 42

DATA VISUALIZATION WITH GGPLOT2

Let’s practice!

slide-43
SLIDE 43

DATA VISUALIZATION WITH GGPLOT2

ggplot2 Objects

slide-44
SLIDE 44

Data Visualization with ggplot2

ggplot2 example

> p

2.0 2.5 3.0 3.5 4.0 4.5 4 5 6 7 8

Length Width Species

setosa versicolor virginica

Iris Sepals

slide-45
SLIDE 45

Data Visualization with ggplot2

Accessing grobs

> library(grid) > g <- ggplotGrob(p) > g TableGrob (6 x 6) "layout": 9 grobs z cells name grob 1 0 (1-6,1-6) background zeroGrob[plot.background..zeroGrob.23938] 2 3 (3-3,3-3) axis-l absoluteGrob[GRID.absoluteGrob.23907] 3 1 (4-4,3-3) spacer zeroGrob[NULL] 4 2 (3-3,4-4) panel gTree[GRID.gTree.23893] 5 4 (4-4,4-4) axis-b absoluteGrob[GRID.absoluteGrob.23900] 6 5 (5-5,4-4) xlab titleGrob[axis.title.x..titleGrob.23910] 7 6 (3-3,2-2) ylab titleGrob[axis.title.y..titleGrob.23913] 8 7 (3-3,5-5) guide-box gtable[guide-box] 9 8 (2-2,4-4) title titleGrob[plot.title..titleGrob.23937]

slide-46
SLIDE 46

Data Visualization with ggplot2

ggplot object

> e <- ggplot() > class(e) [1] "gg" "ggplot" > names(e) [1] "data" "layers" "scales" "mapping" "theme" [6] "coordinates" "facet" "plot_env" "labels"

slide-47
SLIDE 47

Data Visualization with ggplot2

p

> p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point(alpha = 0.3, size = 5, shape = 16) + geom_smooth(method = "lm", se = FALSE) + scale_y_continuous("Width", limits = c(2, 4.5), expand = c(0,0)) + scale_x_continuous("Length", limits = c(4, 8), expand = c(0,0)) + coord_equal() + ggtitle("Iris Sepals") + theme(rect = element_blank()) > names(p) [1] "data" "layers" "scales" "mapping" "theme" [6] "coordinates" "facet" "plot_env" "labels"

slide-48
SLIDE 48

Data Visualization with ggplot2

> p$data Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa ... > p$layers > p$scales > p$mapping > p$theme > p$coordinates > p$facet > p$plot_env > p$labels

slide-49
SLIDE 49

Data Visualization with ggplot2

> p$data > p$layers [[1]] geom_point: na.rm = FALSE stat_identity: na.rm = FALSE position_identity [[2]] geom_smooth: na.rm = FALSE stat_smooth: na.rm = FALSE, method = lm, formula = y ~ x, se = FALSE position_identity > p$scales > p$mapping > p$theme > p$coordinates > p$facet > p$plot_env > p$labels

slide-50
SLIDE 50

Data Visualization with ggplot2

> p$data > p$layers > p$scales <ggproto object: Class ScalesList> add: function clone: function find: function get_scales: function has_scale: function input: function n: function non_position_scales: function scales: list super: <ggproto object: Class ScalesList> > p$mapping > p$theme > p$coordinates > p$facet > p$plot_env > p$labels

slide-51
SLIDE 51

Data Visualization with ggplot2

> p$data > p$layers > p$scales > p$mapping * x -> Sepal.Length * y -> Sepal.Width * colour -> Species > p$theme > p$coordinates > p$facet > p$plot_env > p$labels

slide-52
SLIDE 52

Data Visualization with ggplot2

> p$data > p$layers > p$scales > p$mapping > p$theme List of 1 $ rect: list() ..- attr(*, "class")= chr [1:2] "element_blank" "element"

  • attr(*, "class")= chr [1:2] "theme" "gg"
  • attr(*, "complete")= logi FALSE
  • attr(*, "validate")= logi FALSE

> p$coordinates > p$facet > p$plot_env > p$labels

slide-53
SLIDE 53

Data Visualization with ggplot2

> p$data > p$layers > p$scales > p$mapping > p$theme > p$coordinates <ggproto object: Class CoordFixed, CoordCartesian, Coord> aspect: function distance: function expand: TRUE is_linear: function labels: function limits: list range: function ratio: 1 render_axis_h: function ... > p$facet > p$plot_env > p$labels

slide-54
SLIDE 54

Data Visualization with ggplot2

> p$data > p$layers > p$scales > p$mapping > p$theme > p$coordinates > p$facet facet_null() > p$plot_env > p$labels

slide-55
SLIDE 55

Data Visualization with ggplot2

> p$data > p$layers > p$scales > p$mapping > p$theme > p$coordinates > p$facet > p$plot_env <environment: R_GlobalEnv> > p$labels

slide-56
SLIDE 56

Data Visualization with ggplot2

> p$data > p$layers > p$scales > p$mapping > p$theme > p$coordinates > p$facet > p$plot_env > p$labels $title [1] "Iris Sepals" $x [1] "Sepal.Length" $y [1] "Sepal.Width" $colour [1] "Species"

slide-57
SLIDE 57

Data Visualization with ggplot2

ggplot_build

> p_build <- ggplot_build(p) > names(p_build) [1] "data" "panel" "plot"

slide-58
SLIDE 58

Data Visualization with ggplot2

data

> p_build$data [[1]] colour y x PANEL group shape size fill alpha stroke 1 #F8766D 3.5 5.1 1 1 16 5 NA 0.3 0.5 2 #F8766D 3.0 4.9 1 1 16 5 NA 0.3 0.5 3 #F8766D 3.2 4.7 1 1 16 5 NA 0.3 0.5 4 #F8766D 3.1 4.6 1 1 16 5 NA 0.3 0.5 ... [[2]] colour x y PANEL group fill size linetype weight alpha 1 #F8766D 4.300000 2.864239 1 1 grey60 1 1 1 0.4 2 #F8766D 4.318987 2.879401 1 1 grey60 1 1 1 0.4 3 #F8766D 4.337975 2.894563 1 1 grey60 1 1 1 0.4 4 #F8766D 4.356962 2.909725 1 1 grey60 1 1 1 0.4 ...

slide-59
SLIDE 59

Data Visualization with ggplot2

panel

> p_build$panel $layout PANEL ROW COL SCALE_X SCALE_Y 1 1 1 1 1 1 $shrink [1] TRUE $x_scales $x_scales[[1]] <ScaleContinuousPosition> Range: 4.3 -- 7.9 Limits: 4.3 -- 8 $y_scales $y_scales[[1]] <ScaleContinuousPosition> ...

slide-60
SLIDE 60

Data Visualization with ggplot2

plot

> p_build$plot

2.0 2.5 3.0 3.5 4.0 4.5 4 5 6 7 8

Length Width Species

setosa versicolor virginica

Iris Sepals

slide-61
SLIDE 61

Data Visualization with ggplot2

gtable

> p_build <- ggplot_build(p) > gtab <- ggplot_gtable(p_build) > gtab TableGrob (6 x 6) "layout": 9 grobs z cells name grob 1 0 (1-6,1-6) background zeroGrob[plot.background..zeroGrob.25361] 2 3 (3-3,3-3) axis-l absoluteGrob[GRID.absoluteGrob.25330] 3 1 (4-4,3-3) spacer zeroGrob[NULL] 4 2 (3-3,4-4) panel gTree[GRID.gTree.25316] 5 4 (4-4,4-4) axis-b absoluteGrob[GRID.absoluteGrob.25323] 6 5 (5-5,4-4) xlab titleGrob[axis.title.x..titleGrob.25333] 7 6 (3-3,2-2) ylab titleGrob[axis.title.y..titleGrob.25336] 8 7 (3-3,5-5) guide-box gtable[guide-box] 9 8 (2-2,4-4) title titleGrob[plot.title..titleGrob.25360] > gtab2 <- ggplotGrob(p) # same thing

slide-62
SLIDE 62

Data Visualization with ggplot2

grid.draw

> library(grid) > grid.draw(gtab)

2.0 2.5 3.0 3.5 4.0 4.5 4 5 6 7 8

Length Width Species

setosa versicolor virginica

Iris Sepals

slide-63
SLIDE 63

Data Visualization with ggplot2

gtab

> names(gtab) [1] "grobs" "layout" "widths" "heights" [5] "respect" "rownames" "colnames" "name" [9] "gp" "vp" "children" "childrenOrder"

slide-64
SLIDE 64

Data Visualization with ggplot2

layout

> gtable_show_layout(gtab)

(1, 1) 5.5pt 5.5pt (1, 2) 1grobwidth (1, 3) sum(1grobwidth, 2.75pt) (1, 4) 1null (1, 5) sum(0.2cm, max(sum(0.15cm, 0.6096cm, 0.0762cm, 1.32833232985602cm, 0cm, 0.15cm)), 0.2cm)+0.2cm (1, 6) 5.5pt 5.5pt (2, 1) 1grobheight (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) 1grobheight (3, 1) 0.675675675675676null (3, 2) (3, 3) (3, 4) (3, 5) (3, 6) 0.675675675675676n (4, 1) sum(2.75pt, 1grobheight) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6) sum(2.75pt, 1grobheight) (5, 1) 1grobheight (5, 2) (5, 3) (5, 4) (5, 5) (5, 6) 1grobheight (6, 1) 5.5pt 5.5pt (6, 2) 1grobwidth (6, 3) sum(1grobwidth, 2.75pt) (6, 4) 1null (6, 5) sum(0.2cm, max(sum(0.15cm, 0.6096cm, 0.0762cm, 1.32833232985602cm, 0cm, 0.15cm)), 0.2cm)+0.2cm (6, 6) 5.5pt 5.5pt

slide-65
SLIDE 65

Data Visualization with ggplot2

layout (2)

> gtab$layout t l b r z clip name 9 1 1 6 6 0 on background 1 3 3 3 3 3 off axis-l 2 4 3 4 3 1 off spacer 3 3 4 3 4 2 on panel 4 4 4 4 4 4 off axis-b 5 5 4 5 4 5 off xlab 6 3 2 3 2 6 off ylab 7 3 5 3 5 7 off guide-box 8 2 4 2 4 8 off title

(1, 1) 5.5pt 5.5pt (1, 2) 1grobwidth (1, 3) sum(1grobwidth, 2.75pt) (1, 4) 1null (1, 5) sum(0.2cm, max(sum(0.15cm, 0.6096cm, 0.0762cm, 1.32833232985602cm, 0cm, 0.15cm)), 0.2cm)+0.2cm (1, 6) 5.5pt 5.5pt (2, 1) 1grobheight (2, 2) (2, 3) (2, 4) (2, 5) (2, 6) 1grobheight (3, 1) 0.675675675675676null (3, 2) (3, 3) (3, 4) (3, 5) (3, 6) 0.675675675675676n (4, 1) sum(2.75pt, 1grobheight) (4, 2) (4, 3) (4, 4) (4, 5) (4, 6) sum(2.75pt, 1grobheight) (5, 1) 1grobheight (5, 2) (5, 3) (5, 4) (5, 5) (5, 6) 1grobheight (6, 1) 5.5pt 5.5pt (6, 2) 1grobwidth (6, 3) sum(1grobwidth, 2.75pt) (6, 4) 1null (6, 5) sum(0.2cm, max(sum(0.15cm, 0.6096cm, 0.0762cm, 1.32833232985602cm, 0cm, 0.15cm)), 0.2cm)+0.2cm (6, 6) 5.5pt 5.5pt

slide-66
SLIDE 66

Data Visualization with ggplot2

Update clipping

> gtab$layout$clip [1] "on" "off" "off" "on" "off" "off" "off" "off" "off" > gtab$layout$clip[gtab$layout$name == "panel"] <- "off"

slide-67
SLIDE 67

Data Visualization with ggplot2

Redraw

> library(grid) > grid.draw(gtab)

2.0 2.5 3.0 3.5 4.0 4.5 5 6 7 8

Length Width Species

setosa versicolor virginica

Iris Sepals

slide-68
SLIDE 68

DATA VISUALIZATION WITH GGPLOT2

Let’s practice!

slide-69
SLIDE 69

DATA VISUALIZATION WITH GGPLOT2

gridExtra

slide-70
SLIDE 70

Data Visualization with ggplot2

gridExtra

  • Manage multiple ploing objects
  • Reasons
  • Avoid giant faceed plot
  • Defer ploing
  • Arrange multiple plots in layout
  • Make a multiple page pdf of plots
slide-71
SLIDE 71

Data Visualization with ggplot2

> library(plyr) > my_plots <- dlply(mtcars, .(cyl), function(df) { ggplot(df, aes(mpg, wt)) + geom_point() + xlim(range(mtcars$mpg)) + ylim(range(mtcars$wt)) + ggtitle(paste(df$cyl[1], "cylinders"))})

Build multiple plots

> length(my_plots) [1] 3 > names(my_plots) [1] "4" "6" "8"

slide-72
SLIDE 72

Data Visualization with ggplot2

Plot by position

> my_plots[[2]]

  • 2

3 4 5 10 15 20 25 30 35

mpg wt

6 cylinders

slide-73
SLIDE 73

Data Visualization with ggplot2

Plot by name

> my_plots[["6"]]

  • 2

3 4 5 10 15 20 25 30 35

mpg wt

6 cylinders

slide-74
SLIDE 74

Data Visualization with ggplot2

Combine plots (1)

> library(gridExtra) > grid.arrange(my_plots[[2]], my_plots[[1]], ncol = 2)

  • 2

3 4 5 10 15 20 25 30 35

mpg wt

6 cylinders

  • 2

3 4 5 10 15 20 25 30 35

mpg wt

4 cylinders

slide-75
SLIDE 75

Data Visualization with ggplot2

Combine plots (2)

> do.call(grid.arrange, my_plots)

  • 2

3 4 5 10 15 20 25 30 35

mpg wt

4 cylinders

  • 2

3 4 5 10 15 20 25 30 35

mpg wt

6 cylinders

  • 2

3 4 5 10 15 20 25 30 35

mpg wt

8 cylinders

slide-76
SLIDE 76

Data Visualization with ggplot2

Combine plots (3)

> grid.arrange(p, my_plots[[1]])

2.0 2.5 3.0 3.5 4.0 4.5 4 5 6 7 8

Length Width Species

setosa versicolor virginica

Iris Sepals

  • 2

3 4 5 10 15 20 25 30 35

mpg wt

4 cylinders

slide-77
SLIDE 77

Data Visualization with ggplot2

Why grid.arrange()?

  • You are not able to make manual adjustments
  • Creating many of the same composite plots
  • Slight variations (different dataset or variables)
slide-78
SLIDE 78

DATA VISUALIZATION WITH GGPLOT2

Let’s practice!