iplotr is a fork of Karl Broman's R/qtlcharts R package to create interactive charts aimed at QTL data for use with R/qtl.
This package reformulates the core of qtlcharts
into more generic plotting
functions for any type of data. The plotting functions in this package were
designed to be used like base R plotting functions and use familiar syntax
with similar results.
Interactive plots are particularly useful for exploratory data analysis within R but may also be included in R Markdown documents, an extension of Markdown. knitr can be used to write simply-marked-up text with chunks of R code, and output is converted to an html file for viewing in a web browser.
To include interactive plots within an R Markdown document, you just need to include the relevant calls to the R function within a code chunk as if you were creating any other type of plot.
For more information on R Markdown and knitr, see Karl's knitr in a knutshell tutorial.
# knitr::opts_chunk$set(fig.width=8, fig.height=6, message=FALSE) library('iplotr')
One of the most common types of plots, scatter plots, is created with the
function iscatter
.
iscatter(1:5, col = 1:5, cex = 5)
The base R function plot
will create a similar graph without the interactivity.
Users familiar with R's base plotting functions will also be familiar with some
of the parameters: col
, cex
, xlim
/ylim
, etc.
However, there are some key differences. First, col
is not a vector the same
length as x
for coloring individual points. Rather col
is a vector having
length equal to the number of desired groups. Points are colored according to
the group
to which they are assigned.
iscatter(1:5, group = c(1, 1, 1, 2, 3), col = 4:6, cex = 5)
Another difference is the labels
parameter. This argument appears in most of
the interactive plots in this package and is used to label points and provide
some useful information about each point.
labels
can be passed as either a character vector of labels for each point or
as a list
of labels which will be parsed and displayed in a convenient way.
labels = list( ' ' = rownames(mtcars), ## use whitespace for no label ("Label: ") MPG = mtcars$mpg, 'No. of gears' = mtcars$gear ) with(mtcars, { iscatter(wt, mpg, group = cyl, col = 1:3, labels = labels, cex = 5, main = 'Motor Trend car road tests') })
You can also use html tags in labels
for better display.
v_labels <- list( ' ' = rownames(ivolcano), 'log<sub>2</sub>(FC)' = round(ivolcano$logFC, 2), '<i>p</i>-value' = sprintf('<font color=red>%s</font>', format.pval(ivolcano$pval, digits = 2, eps = 0.05)) ) with(ivolcano, { iscatter(logFC, -log10(pval), col = c('lightgrey','green'), group = pval < 0.05 & abs(logFC) > 1, labels = v_labels) })
Dot plots are also useful to view data interactively with added information
on each point. There is no x
parameter here--instead the vector of y
values
are grouped along the x-axis using a vector of group
data. Continuous values
of y
may be arranged by group
resembling a pseudo boxplot-style plot.
You can double-click any point to increase the size for quick identification later.
with(mtcars, idot(gear, mpg, labels = labels))
Points can further be colored using group
which should be a logical vector.
with(ivolcano, { idot(substr(rownames(ivolcano), 1, 1), logFC, group = cut(pval, c(0, 0.05, 0.1, 1)), xlab = 'Treatment arm', labels = v_labels) })
icorr
creates a heat map from a matrix of numeric data with rows of observations
and columns of variables where each cross-section of the matrix is (optionally)
linked to a scatter plot of the underlying data.
icorr(mtcars, scatterplots = FALSE)
group
, col
, and labels
arguments are used the same as in iscatter
.
Additionally, the matrix can be clustered and the rows/columns reordered.
set.seed(1) dat <- replicate(50, mtcars[, sample(1:11, 1), drop = FALSE]) dat <- do.call('cbind', dat) icorr(dat, cluster = TRUE, group = mtcars$cyl, labels = rownames(mtcars))
This tree map is similar to idot
but also features a search bar to find
specific points easily. Continuous values of y
may be arranged by group
resembling a pseudo boxplot-style plot.
set.seed(1) n <- 100 ng <- 5 gr <- sort(sample(LETTERS[1:ng], n, replace = TRUE)) yv <- kinda_sort(runif(n, -1, 1), n = n / 2) * 100 itree(yv, gr, main = 'Subject response from baseline', ylim = c(-100,100), xlab = 'Treatment arm', ylab = '% change')
These curves showing observations with repeated measures over time can optionally
be linked to up to two interactive scatter plots. labels
and group
vectors are
used to provide labels and color both curves and points.
As individual curves are selected and highlighted, the corresponding data points on the scatter plots are also identified. Similarly, by highlighting points on either scatter plot, the curve linked to that observation is also highlighted.
set.seed(1) n <- 25 mm <- cbind(runif(n, 0, 10), runif(n, 10, 50), runif(n, 30, 60), runif(n, 40, 80), runif(n, 60, 100)) mm <- mm[order(mm[, 5]), ] x1 <- cbind(1:n, kinda_sort(rnorm(n), n / 2)) x2 <- cbind(1:n, kinda_sort(sample(1:100, n), n / 2)) icurve( mat = mm, group = 1:25 %% 5 == 0, iscatter1 = x1, iscatter2 = x2, labels = list( Patient = 1:25, Disease = sample(c('NHL','HL'), 25, replace = TRUE) ), plotOpts = list( curves_xlab = 'Response evaluation time point', curves_ylab = '% response', scat1_xlab = 'Patient', scat1_ylab = 'Lab var 1', scat2_xlab = 'Patient', scat2_ylab = 'Lab var 2') )
Each type of plot has its own set of options and customizations that affect
the appearance of individual aesthetic elements. Some options such as
pointcolor
, pointsize
, or rectcolor
are common among all or most plots,
but others apply only to specific plots.
See ?iplotOpts
for a list of the plots in this package as well as links to
the lists of options for each plot.
To save an interactive plot (html widget), you can use the saveWidget
function from the htmlwidgets package. It is
recommended to use selfcontained = TRUE
(default) when saving widgets as
html files--file sizes will be larger, but the plots will not depend on
external resources to be displayed properly.
htmlwidgets::saveWidget(file = './scatter_plot.html', iscatter(rnorm(10), runif(10)) )
install.packages('devtools') devtools::install_github('raredd/iplotr', build_vignettes = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.