knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(pineplot)

Loading the Data

For this example we will be using the Cleveland heart disease dataset. The dataset contains 14 attributes (as columns) across 303 subjects (as rows). The attributes were selected for their relevance to heart disease and include variables such as age, sex, cholesteral level, and maximum heart rate achieved.

data(heart)
colnames(heart)
attr(heart, "format") # column information

Preprocessing

We will be examining the interaction between several variables while controlling for factors of sex and age. Levels for the sex attribute are already defined in the dataset (1 = male; 0 = female). For the age attribute, we define 3 levels (i.e. age groups) using the \code{cut} function. Correlation matrices are then calculated for each of the subsets defined by the sex and age factors.

heart['age'] <- cut(heart[['age']], 3)
heart_mat <- heart[c('trestbps', 'chol', 'fbs', 'restecg', 'thalach')]
heart_subsets <- split(heart_mat, list(heart$sex, heart$age))
heart_subsets <- lapply(heart_subsets, cor)

Annotation Functions

We define a callback function that generates a text grob (GRaphical OBject) given the heatmap objects and a name argument. The callback function will be used to label the heatmaps. The functionality for creating text grobs and other basic grobs is provided by the grid package.

library(grid)

add_label <- function(panel, name){
  text <- paste0('Age:\n', sub('.*\\.', '', name))
  textGrob(text, x=0, y=.8, just='left')
}

Pineplot Generation

Two pineplots are generated, one for females and one for males. The pineplots are stored in a list for later arrangment using the grid package. When calling generate_pineplot, we pass the callback function as an argument.

pineplots <- list()
for (sex in c(0, 1)) {
  sex_indices <- as.numeric(sub('\\..*', '', names(heart_subsets)))
  heart_by_sex <- heart_subsets[which(sex_indices == sex)]
  pp <- generate_pineplot(
    heart_by_sex,
    customize_fn = add_label,
    breaks=c(-1, -.5, 0, .5, 1),
    limits=c(-1, 1),
    low="blue", mid="yellow", high="red",
    legend.scale=1.2
  )
  pineplots <- c(pineplots, list(pp))
}

The grid.arrange function provided by gridExtra is used to lay the pine plots out side-by-side for easy comparison.

library(gridExtra)

grid.arrange(pineplots[[1]], pineplots[[2]], nrow=1)


klovens/pineplot documentation built on Nov. 4, 2019, 3:53 p.m.