Permalink
Browse files

curves_w_2scatter: working towards a new chart

  - will have two scatterplots linked to a plot with a bunch of curves
  • Loading branch information...
1 parent f2145d2 commit 4b93102ead26a33b439c08c08117cb39c8fe59c7 @kbroman committed Mar 4, 2014
Showing with 232 additions and 4 deletions.
  1. +3 −3 DESCRIPTION
  2. +1 −1 Makefile
  3. +1 −0 NAMESPACE
  4. +89 −0 R/curves_w_2scatter.R
  5. +43 −0 inst/charts/curves_w_2scatter.coffee
  6. +26 −0 inst/charts/curves_w_2scatter.js
  7. +69 −0 man/curves_w_2scatter.Rd
View
@@ -1,13 +1,13 @@
Package: qtlcharts
-Version: 0.1-9
-Date: 20 Feb 2014
+Version: 0.1-10
+Date: 4 Mar 2014
Title: Interactive graphics for QTL experiments
Author: Karl W Broman <kbroman@biostat.wisc.edu>
Maintainer: Karl W Broman <kbroman@biostat.wisc.edu>
Description: Web-based interactive charts (using D3.js) for the analysis of
experimental crosses to identify genetic loci (quantitative trait loci,
QTL) contributing to variation in quantitative traits.
-Depends: R (>= 2.14.0), qtl, RJSONIO, graphics, stats, utils
+Depends: R (>= 3.0.2), qtl (>= 1.30-4), RJSONIO, graphics, stats, utils
Suggests: devtools, roxygen2
License: MIT + file LICENSE
URL: http://github.com/kbroman/qtlcharts
View
@@ -71,7 +71,7 @@ ${PANEL_DIR}/*/test/d3-tip.css: inst/d3-tip/d3-tip.css
#------------------------------------------------------------
# javascript for the real charts
-jscharts: ${CHART_DIR}/iplotScanone_noeff.js ${CHART_DIR}/iplotScanone_pxg.js ${CHART_DIR}/iplotScanone_ci.js ${CHART_DIR}/iplotPXG.js ${CHART_DIR}/corr_w_scatter.js ${CHART_DIR}/manyboxplots.js
+jscharts: ${CHART_DIR}/iplotScanone_noeff.js ${CHART_DIR}/iplotScanone_pxg.js ${CHART_DIR}/iplotScanone_ci.js ${CHART_DIR}/iplotPXG.js ${CHART_DIR}/corr_w_scatter.js ${CHART_DIR}/manyboxplots.js ${CHART_DIR}/curves_w_2scatter.js
${CHART_DIR}/%.js: ${CHART_DIR}/%.coffee
coffee ${COFFEE_ARGS} -b $^
View
@@ -1,6 +1,7 @@
# Generated by roxygen2 (4.0.0): do not edit by hand
export(corr_w_scatter)
+export(curves_w_2scatter)
export(iplotPXG)
export(iplotScanone)
export(manyboxplots)
View
@@ -0,0 +1,89 @@
+# curves_w_2scatter.R
+# Karl W Broman
+
+#' Plot of a bunch of curves, linked to points in two scatterplots
+#'
+#' Creates an interactive graph with a panel having a number of curves
+#' (say, a phenotype measured over time) linked to two scatter plots
+#' (say, of the first vs middle and middle vs last times).
+#'
+#' @param curveMatrix Matrix (dim n_ind x n_times) with outcomes
+#' @param times Vector (length n_times) with time points for the columns of curveMatrix
+#' @param scatter1 Matrix (dim n_ind x 2) with data for the first scatterplot
+#' @param scatter2 Matrix (dim n_ind x 2) with data for the second scatterplot
+#' @param file Optional character vector with file to contain the output
+#' @param onefile If TRUE, have output file contain all necessary javascript/css code
+#' @param openfile If TRUE, open the plot in the default web browser
+#' @param title Character string with title for plot
+#' @param legend Character vector with text for a legend (to be
+#' combined to one string with \code{\link[base]{paste}}, with
+#' \code{collapse=''})
+#' @param \dots Additional arguments passed to the \code{\link[RJSONIO]{toJSON}} function
+#' @return Character string with the name of the file created.
+#' @export
+#' @examples
+#' # random growth curves, based on some data
+#' times <- 1:16
+#' n <- 100
+#' start <- rnorm(n, 5.2, 0.8)
+#' slope1to5 <- rnorm(n, 2.6, 0.5)
+#' slope5to16 <- rnorm(n, 0.24 + 0.09*slope1to5, 0.195)
+#' y <- matrix(ncol=16, nrow=n)
+#' y[,1] <- start
+#' for(j in 2:5)
+#' y[,j] <- y[,j-1] + slope1to5
+#' for(j in 6:16)
+#' y[,j] <- y[,j-1] + slope5to16
+#' y <- y + rnorm(prod(dim(y)), 0, 0.35)
+#'
+#' # Make the plot
+#' curves_w_2scatter(y, times, y[,c(1,5)], y[,c(5,16)])
+#' @seealso \code{\link{corr_w_scatter}}
+curves_w_2scatter <-
+function(curveMatrix, times, scatter1, scatter2,
+ file, onefile=FALSE, openfile=TRUE, title="", legend, ...)
+{
+ if(missing(file))
+ file <- tempfile(tmpdir=tempdir(), fileext='.html')
+ else file <- path.expand(file)
+
+ if(file.exists(file))
+ stop('The file already exists; please remove it first: ', file)
+
+ n.ind <- nrow(curveMatrix)
+ n.times <- ncol(curveMatrix)
+ if(length(times) != n.times)
+ stop("length(times) != ncol(curveMatrix)")
+ if(nrow(scatter1) != n.ind)
+ stop("nrow(scatter1) != nrow(curveMatrix)")
+ if(nrow(scatter2) != n.ind)
+ stop("nrow(scatter2) != nrow(curveMatrix)")
+
+ write_html_top(file, title=title)
+
+ link_d3(file, onefile=onefile)
+ link_d3tip(file, onefile=onefile)
+ link_panel('curvechart', file, onefile=onefile)
+ link_panel('scatterplot', file, onefile=onefile)
+ link_chart('curves_w_2scatter', file, onefile=onefile)
+
+ append_html_middle(file, title, 'chart')
+
+ dimnames(curveMatrix) <- names(times) <- dimnames(scatter1) <- dimnames(scatter2) <- NULL
+
+ append_html_jscode(file, 'curve_data = ', toJSON(list(x=times, data=curveMatrix), ...), ';')
+ append_html_jscode(file, 'scatter1_data = ', toJSON(scatter1, ...), ';')
+ append_html_jscode(file, 'scatter2_data = ', toJSON(scatter2, ...), ';')
+ append_html_jscode(file, 'curves_w_2scatter(curve_data, scatter1_data, scatter2_data);')
+
+ if(missing(legend) || is.null(legend))
+ legend <- c('Insert a legend here. ',
+ 'Really; I mean it!')
+ append_legend(legend, file)
+
+ append_html_bottom(file)
+
+ if(openfile) browseURL(file)
+
+ invisible(file)
+}
@@ -0,0 +1,43 @@
+# curves_w_2scatter: Plot of a bunch of curves, linked to points in two scatterplots
+# Karl W Broman
+
+curves_w_2scatter = (curve_data, scatter1_data, scatter2_data) ->
+
+ htop = 500
+ hbot = 500
+ width = 1000
+ margin = {left:60, top:40, right:40, bottom: 40, inner:5}
+ totalh = htop + hbot + 2*(margin.top + margin.bottom)
+ totalw = width + margin.left + margin.right
+ wtop = (width - margin.left - margin.right)/2
+
+ svg = d3.select("div#chart")
+ .append("svg")
+ .attr("height", totalh)
+ .attr("width", totalw)
+
+ myscatterplot1 = scatterplot().width(wtop)
+ .height(htop)
+
+ myscatterplot2 = scatterplot().width(wtop)
+ .height(htop)
+
+ g_scat1 = svg.append("g")
+ .attr("id", "scatterplot1")
+ .datum(scatter1_data)
+ .call(myscatterplot1)
+
+ g_scat2 = svg.append("g")
+ .attr("id", "scatterplot2")
+ .attr("transform", "translate(#{wtop+margin.left+margin.right},0)")
+ .datum(scatter2_data)
+ .call(myscatterplot2)
+
+ mycurvechart = curvechart().width(width)
+ .height(hbot)
+
+ g_curves = svg.append("g")
+ .attr("id", "curvechart")
+ .attr("transform", "translate(0,#{htop+margin.top+margin.bottom})")
+ .datum(curve_data)
+ .call(mycurvechart)

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,69 @@
+% Generated by roxygen2 (4.0.0): do not edit by hand
+\name{curves_w_2scatter}
+\alias{curves_w_2scatter}
+\title{Plot of a bunch of curves, linked to points in two scatterplots}
+\usage{
+curves_w_2scatter(curveMatrix, times, scatter1, scatter2, file,
+ onefile = FALSE, openfile = TRUE, title = "", legend, ...)
+}
+\arguments{
+ \item{curveMatrix}{Matrix (dim n_ind x n_times) with
+ outcomes}
+
+ \item{times}{Vector (length n_times) with time points for
+ the columns of curveMatrix}
+
+ \item{scatter1}{Matrix (dim n_ind x 2) with data for the
+ first scatterplot}
+
+ \item{scatter2}{Matrix (dim n_ind x 2) with data for the
+ second scatterplot}
+
+ \item{file}{Optional character vector with file to
+ contain the output}
+
+ \item{onefile}{If TRUE, have output file contain all
+ necessary javascript/css code}
+
+ \item{openfile}{If TRUE, open the plot in the default web
+ browser}
+
+ \item{title}{Character string with title for plot}
+
+ \item{legend}{Character vector with text for a legend (to
+ be combined to one string with \code{\link[base]{paste}},
+ with \code{collapse=''})}
+
+ \item{\dots}{Additional arguments passed to the
+ \code{\link[RJSONIO]{toJSON}} function}
+}
+\value{
+Character string with the name of the file created.
+}
+\description{
+Creates an interactive graph with a panel having a number of curves
+(say, a phenotype measured over time) linked to two scatter plots
+(say, of the first vs middle and middle vs last times).
+}
+\examples{
+# random growth curves, based on some data
+times <- 1:16
+n <- 100
+start <- rnorm(n, 5.2, 0.8)
+slope1to5 <- rnorm(n, 2.6, 0.5)
+slope5to16 <- rnorm(n, 0.24 + 0.09*slope1to5, 0.195)
+y <- matrix(ncol=16, nrow=n)
+y[,1] <- start
+for(j in 2:5)
+ y[,j] <- y[,j-1] + slope1to5
+for(j in 6:16)
+ y[,j] <- y[,j-1] + slope5to16
+y <- y + rnorm(prod(dim(y)), 0, 0.35)
+
+# Make the plot
+curves_w_2scatter(y, times, y[,c(1,5)], y[,c(5,16)])
+}
+\seealso{
+\code{\link{corr_w_scatter}}
+}
+

0 comments on commit 4b93102

Please sign in to comment.