%\VignetteEngine{knitr::knitr} %\VignetteIndexEntry{10 Visualization in R -- Slides} \documentclass[xcolor=dvipsnames]{beamer} \usepackage{BioconductorSlides} \hypersetup{colorlinks,linkcolor=,urlcolor=Blue} \AtBeginSection[] { \begin{frame}{Outline} \tableofcontents[currentsection] \end{frame} } \begin{document} <>= library(knitr) opts_chunk$set(tidy=FALSE) @ \title{Visualization} \author{Martin Morgan (\href{mailto:mtmorgan@fhcrc.org}{mtmorgan@fhcrc.org}) \\ Fred Hutchinson Cancer Research Center \\ Seattle, WA} \date{7 February 2014} \maketitle \begin{frame}[fragile]{Visualization} \begin{columns}[t] \column{.5\textwidth} General \begin{itemize} \item Base graphics \item \Rpkg{lattice} \item \Rpkg{ggplot2} \end{itemize} \column{.5\textwidth} \Bioconductor \begin{itemize} \item \Biocpkg{Gviz}, \Biocpkg{ggbio} \item \Biocpkg{Rgraphviz}, \Biocpkg{RCytoscape}, \Biocpkg{RedeR} \end{itemize} Interactive \begin{itemize} \item \Rpkg{shiny}! \end{itemize} \end{columns} \href{http://accidental-art.tumblr.com/archive}{Accidental aRt} \end{frame} \begin{frame}[fragile]{Base graphics} \begin{columns}[t] \column{.5\textwidth} One plot \begin{itemize} \item Start a \Rfunction{plot}, \Rfunction{matplot}, \Rfunction{hist}, \Rfunction{boxplot}, \ldots, specifying options \item Add \Rfunction{legend}, \Rfunction{points}, \ldots \item Help: \Rcode{?plot}, \Rcode{?par} \end{itemize} \column{.5\textwidth} Several plots \begin{itemize} \item Layout with \Rcode{par(mfcol=c(1, 2))}, \Rcode{screen}, \ldots \item Create single plots \item Restore original \Rcode{par} settings \end{itemize} \end{columns} \bigskip\par \end{frame} \begin{frame}[fragile]{Base graphics} <>= fl <- system.file(package="SummerX", "extdata", "abc.csv") stopifnot(file.exists(fl)) abc <- read.csv(fl, row.names=1) fl <- system.file(package="SummerX", "extdata", "BRFSS-subset.csv") stopifnot(file.exists(fl)) brfss <- read.csv(fl) @ <>= fileName <- file.choose() # 'abc.csv' abc <- read.csv(fileName, row.names=1) @ \begin{columns} \column{.5\textwidth} <>= ## Create a plot from a ## matrix matplot(t(abc), type="l", lty=1, lwd=3, xlab="Cycle", ylab="Count", cex.lab=2) ## Add a legend legend("topright", legend=rownames(abc), lty=1, lwd=3, col=1:5, cex=1.8) @ \column{.5\textwidth} \includegraphics[width=\textwidth]{figures/abc} \end{columns} \end{frame} \begin{frame}[fragile]{Base graphics} Data: US Center for Disease Control's Behavioral Risk Factor Surveillance System (\href{http://www.cdc.gov/brfss/}{BRFSS}) <>= fl <- system.file(package="SummerX", "extdata", "BRFSS-subset.csv") brfss <- read.csv(fl) brfss2010 <- brfss[brfss$Year == "2010",] @ <>= brfss <- read.csv(file.choose()) # 'BRFSS-subset.csv' brfss2010 <- brfss[brfss$Year == "2010",] @ Plot <>= ## set layout, capture old options opar <- par(mfcol=c(1, 2)) plot(sqrt(Weight) ~ Height, brfss2010[brfss2010$Sex == "Female", ], main="2010, Female") plot(sqrt(Weight) ~ Height, brfss2010[brfss2010$Sex == "Male", ], main="2010, Male") ## restore old options par(mfcol=c(1, 1)) @ \end{frame} \begin{frame}{Base graphics} \includegraphics[width=\textwidth]{figures/brfss-base} \end{frame} \begin{frame}{\Rpkg{lattice} graphics} \begin{itemize} \item Main functions like \Rfunction{xyplot}, \Rfunction{dotplot}, \Rfunction{contourplot} \item `Panel' functions that do the work, e.g., \Rfunction{panel.xyplot}, \Rfunction{panel.violin} \item Help: \Rcode{?xyplot} for working with the plot overall, \Rcode{?panel.xyplot} (and similar) for individual panels. \end{itemize} \end{frame} \begin{frame}[fragile]{\Rpkg{lattice} graphics} <>= fl <- system.file(package="SummerX", "extdata", "BRFSS-subset.csv") brfss <- read.csv(fl) @ <>= brfss <- read.csv(file.choose()) # 'BRFSS-subset.csv' @ <>= library(lattice) xyplot(sqrt(Weight) ~ Height | Sex, brfss2010) @ \includegraphics[width=\textwidth]{figures/brfss-xyplot} \end{frame} \begin{frame}[fragile]{\Rpkg{lattice} graphics} <>= bwplot(sqrt(Weight) ~ factor(Year) | Sex, brfss, panel=panel.violin) @ \includegraphics[width=\textwidth]{figures/brfss-violin} \end{frame} \begin{frame}{\Rpkg{ggplot2} graphics} \begin{itemize} \item Create a plot with \Rfunction{ggplot} \item Add \emph{layers} (e.g., \Rfunction{geom\_density} and \emph{aesthetics} (e.g., \Rfunction{aes}) \end{itemize} \end{frame} \begin{frame}[fragile]{\Rpkg{ggplot2} graphics} <>= fl <- system.file(package="SummerX", "extdata", "BRFSS-subset.csv") brfss <- read.csv(fl) brfss2010 <- brfss[brfss$Year == "2010",] @ <>= brfss <- read.csv(file.choose()) # 'BRFSS-subset.csv' brfss2010 <- brfss[brfss$Year == "2010",] @ <>= library(ggplot2) ggplot(brfss2010) + geom_density(alpha=.2) + aes(sqrt(Weight), fill=Sex) @ \includegraphics[width=\textwidth]{figures/brfss-geom_density} \end{frame} \begin{frame}{Genomic Visualization} \begin{itemize} \item Very flexible base, \Rpackage{lattice}, and \Rpkg{ggplot2} facilities \item Ranges and genomes: \Biocpkg{Gviz}, \Biocpkg{ggbio} \item Graphs and networks: \Biocpkg{Rgraphviz}, \Biocpkg{RCytoscape} \end{itemize} \end{frame} \begin{frame}[fragile]{Interactive Visualization} \Rpkg{shiny} provides an easy way to create interactive graphics. Explore the \emph{Volcano!} application developed in class through the source code in the package. <>= system.file(package="SummerX", "shiny") @ \end{frame} \end{document}