Sparkline
# Preliminary, load packages and build a demo table with the sparkline code merged in
library(shiny)
library(DT)
library(data.table)
library(sparkline)
## Create demo data sets
my_mtcars <- data.table(mtcars, keep.rownames = TRUE)
names(my_mtcars)[1] <- 'car_id'
set.seed(0)
data_for_sparklines <- data.table(car_id = rep(my_mtcars$car_id, 5),
category = 1:5,
value = runif(160))
sparkline_html <- data_for_sparklines[, .(sparkbar = spk_chr(value, type = 'bar')), by = 'car_id']
my_mtcars <- merge(my_mtcars, sparkline_html, by = 'car_id')
spk_add_deps(datatable(my_mtcars, escape = FALSE))
## Create demo data sets
my_mtcars <- data.table(mtcars, keep.rownames = TRUE)
names(my_mtcars)[1] <- 'car_id'
set.seed(0)
data_for_sparklines <- data.table(car_id = rep(my_mtcars$car_id, 5),
category = 1:5,
value = runif(160))
sparkline_html <- data_for_sparklines[, .(sparkbar = spk_chr(value, type = 'line')), by = 'car_id']
my_mtcars <- merge(my_mtcars, sparkline_html, by = 'car_id')
spk_add_deps(datatable(my_mtcars, escape = FALSE))
Superheat
library(superheat)
superheat(mtcars,
# scale the matrix columns
scale = TRUE)

# generate the plot:
superheat(mtcars,
# retain original order of rows/cols
pretty.order.rows = TRUE,
pretty.order.cols = TRUE,
# scale the matrix columns
scale = TRUE)

# generate the plot:
superheat(mtcars,
# order the rows by miles per gallon
order.rows = order(mtcars$mpg),
# scale the matrix columns
scale = TRUE)

superheat(mtcars,
# scale the matrix columns
scale = TRUE,
# change the color
heat.col.scheme = "red")

superheat(mtcars,
# scale the matrix columns
scale = TRUE,
# change the color (#b35806 = brown and #542788 = purple)
heat.pal = c("#b35806", "white", "#542788"))

superheat(mtcars,
# scale the matrix columns
scale = TRUE,
# change the color (#b35806 = brown and #542788 = purple)
heat.pal = c("#b35806", "white", "#542788"),
heat.pal.values = c(0, 0.5, 1))

superheat(mtcars,
# scale the matrix columns
scale = TRUE,
heat.lim = c(-1, 2))

superheat(mtcars,
# scale the matrix columns
scale = TRUE,
heat.lim = c(-1, 2),
extreme.values.na = FALSE)

# replace some values with missing values
mtcars.missing <- mtcars
mtcars.missing[sample(1:nrow(mtcars), 5),
sample(1:ncol(mtcars), 5)] <- NA
superheat(mtcars.missing,
# scale the matrix
scale = T)

superheat(mtcars,
# scale the matrix columns
scale = TRUE,
# add row dendrogram
row.dendrogram = TRUE)

# plot a super heatmap
superheat(dplyr::select(mtcars, -mpg),
# scale the variables/columns
scale = T,
# add mpg as a scatterplot next to the rows
yr = mtcars$mpg,
yr.axis.name = "miles per gallon")

# plot a super heatmap
superheat(dplyr::select(mtcars, -mpg),
# scale the variables/columns
scale = T,
# add mpg as a line plot next to the rows
yr = mtcars$mpg,
yr.axis.name = "miles per gallon",
yr.plot.type = "bar",
# order the rows by mpg
order.rows = order(mtcars$mpg))

# plot a super heatmap
superheat(dplyr::select(mtcars, -mpg),
# scale the variables/columns
scale = T,
# add mpg as a line plot next to the rows
yr = mtcars$mpg,
yr.axis.name = "miles per gallon",
yr.plot.type = "line",
# order the rows by mpg
order.rows = order(mtcars$mpg))

# plot a super heatmap
superheat(dplyr::select(mtcars, -mpg),
# scale the variables/columns
scale = T,
# add mpg as a scatterplot next to the rows
yr = mtcars$mpg,
yr.axis.name = "miles per gallon",
# add correlation between each variable and miles per gallon
yt = cor(mtcars)[-1,"mpg"],
yt.plot.type = "bar",
yt.axis.name = "Correlation\nwith mpg")

superheat(X = mtcars, # heatmap matrix
# scale the matrix columns
scale = TRUE,
# add text matrix
X.text = round(as.matrix(mtcars), 1),
X.text.size = 4)

# set the text colors
# identify all scaled values that fall below -0.3
mtcars.col <- scale(mtcars) < -0.3
# set all values that satisfy the condition to "white"
mtcars.col <- gsub("TRUE", "white", mtcars.col)
# set all values that do not satisfy the condition to "black"
mtcars.col <- gsub("FALSE", "black", mtcars.col)
# convert to matrix
mtcars.col <- matrix(mtcars.col, ncol = ncol(mtcars))
superheat(X = mtcars, # heatmap matrix
# scale the matrix columns
scale = TRUE,
# add text matrix
X.text = round(as.matrix(mtcars), 1),
X.text.col = mtcars.col,
X.text.size = 4)
