%\VignetteEngine{knitr::knitr} %\VignetteIndexEntry{The km.plot and sync.ylab.widths functions} %\VignetteDepends{tidyverse, figuRes2}

Example 1: Juxtaposing two Kaplan-Meier graphics and their At Risk tables

Intialize a session:

remove(list=ls())
require(figuRes2)
require(survival)
require(ggplot2)
require(plyr)
require(scales)
default.settings()
## my.path is set to:C:/Users/eri7441/AppData/Local/Temp/2/Rtmp8o65bH/Rbuild930512f5617/figuRes2/vignettes
## dd is set to:C:/Users/eri7441/AppData/Local/Temp/2/Rtmp8o65bH/Rbuild930512f5617/figuRes2/vignettes/dddata/
## cd is set to:C:/Users/eri7441/AppData/Local/Temp/2/Rtmp8o65bH/Rbuild930512f5617/figuRes2/vignettes/code/
## od is set to:C:/Users/eri7441/AppData/Local/Temp/2/Rtmp8o65bH/Rbuild930512f5617/figuRes2/vignettes/output/
## logd is set to:C:/Users/eri7441/AppData/Local/Temp/2/Rtmp8o65bH/Rbuild930512f5617/figuRes2/vignettes/log/
## The default page dimension is set to landscape: 11 inches wide by 8.5 inches tall.
## The default page left and right page margins: 0.75 inches and 0.75 inches, respectively.
## The default top an bottom margins: 0.9 inches and 1.25 inches, respectively.
## The region available for graphics/tables is 9.5 inches wide by 6.35 inches tall.
## 
## The default theme: theme_bw

A data set included in the figuRes2 package is loaded. Note that this data contains one row per subject and has a censor column and a centime column.

data(km.data)
working.df <- km.data
head(working.df)
##   CENTREID SUBJID AGE SEX    TRTGRP COUNTRY        REGION CENSOR CENTIME.DAY
## 1    58785      1  64   M Treatment  Canada North America      0        1504
## 2    58785      2  73   M Treatment  Canada North America      0        1534
## 3    58785      3  39   M   Placebo  Canada North America      0        1485
## 4    58785      4  63   M Treatment  Canada North America      0        1415
## 5    58785      5  59   M Treatment  Canada North America      0        1451
## 6    58785      6  73   M   Placebo  Canada North America      0        1469
##   CENTIME.MON
## 1    49.41273
## 2    50.39836
## 3    48.78850
## 4    46.48871
## 5    47.67146
## 6    48.26283

The Goal: Create Kaplan-Meier plots for Males and Females and juxtapose these.

Create the graphic components

The km.plot function returns a list of objects. The first two objects are ggplot objects for the Kaplan-Meier graphic and corresponding at risk table.

km.M <- km.plot(parent.df = subset(working.df, SEX=="M"),
                censor.col = "CENSOR", 
                centime.col = "CENTIME.MON",
                category.col = "TRTGRP",
                category.palette = c("red", "blue"),
                at.risk.palette = c("red","blue"),                       
                linetype.palette = c("solid","dotted"), 
                x.label = "Time Since Randomization (months)", 
                x.limits=c(-3,48),
                x.ticks=seq(0,48,6),
                y.limits=c(0,.25), 
                y.ticks=seq(0,.25,.05))

Inspecting the Kaplan-Meier Graphic:

km.M[[1]]

Kaplan-Meier Graphic for Males

Inspecting the At Risk Table:

km.M[[2]]

Corresponding At Risk Table for Males

A similar call for the females:

km.F <- km.plot(parent.df = subset(working.df, SEX=="F"),
                censor.col = "CENSOR", 
                centime.col = "CENTIME.MON",
                category.col = "TRTGRP",
                category.palette = c("red", "blue"),
                at.risk.palette = c("red","blue"),
                linetype.palette = c("solid","dotted"), 
                x.label = "Time Since Randomization (months)",
                x.limits=c(-3,48),
                x.ticks=seq(0,48,6),
                y.limits=c(0,.25), 
                y.ticks=seq(0,.25,.05))

First object:

km.F[[1]]

Kaplan-Meier Graph for Females

Second object:

km.F[[2]]

Number at Risk Table for Females

Syncing the widths of the figures

In this step we combine tasks of aligning the y axes of the KM curves & tables. The sync.ylab.widths function takes a list of ggplot objects and returns a list of the same length contain gtable objects. These can objects are different than ggplot objects. They can be visually inspected with grid.draw (grid package) and can be processed by the build.page function (as this is merely a wrapper for the grid.arrange function (gridExtra package)).

In this example, the list of ggplot objects being supplied to sync.ylab.widths are manipulations of the graphs already reviewed. In particular:

comeback.M <- sync.ylab.widths(list(
  km.M[[1]]+
         ggtitle("Kaplan Meier-Plot of Time to\nFirst MACE: Males") +
    guides(color=F, linetype=F), 
  km.M[[2]]+labs(x=NULL, y="At Risk")
  ))
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
comeback.F <- sync.ylab.widths(list(
  km.F[[1]]+
    ggtitle("Kaplan Meier-Plot of Time to\nFirst MACE: Females") +
    guides(color=F,linetype=F) +
    theme(axis.ticks.y=element_line(color="white")) + 
    labs(y=NULL) +
    scale_y_continuous(labels=NULL, limits=c(0,.25), breaks=seq(0,.25,.05)),
  km.F[[2]]+
    labs(x=NULL, y=NULL) +
    scale_y_discrete(labels=NULL))
  )
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.

Assembling the page and discussion

One needs to iterate with minor changes until the final product is visually appealing. One needs to consider the dimensions allocated to the interior.h and interior.w below as well. E.g., this allocation seems to work well for the data set. However, if the treatment labels were shorter, the allocation of interior.w would need to change. Similarly if more arms were included, more rows in the At Risk table would demand a different distribution of interior.h.

In manipulating the plot margins keep in mind that overlapping graphics can obscure the edges of adjacent graphics/tables. E.g., note above that c(-3, 48) was passed to x.limits instead of c(0, 48). With the latter in use, portions of the text in the At Risk table were obscured.

build.page(interior.h = c(.8, .2),
           interior.w = c(.6,.4),
           ncol=2, nrow=2,
           interior = list(comeback.M[[1]], comeback.F[[1]],
                           comeback.M[[2]], comeback.F[[2]]))

A Figure that arranges four graphics

build.page(interior.h = c(.8, .2),
           interior.w = c(.5,.5),
           ncol=2, nrow=2,
           interior = list(comeback.M[[1]], comeback.F[[1]],
                           comeback.M[[2]], comeback.F[[2]]))

A Figure that better arranges four graphics