Interactive Q-Q Plots in R using Plotly

[This article was first published on R – Modern Data, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

In a recent blog post, I introduced the new R package, manhattanly, which creates interactive manhattan plots using the plotly.js engine.

In this post, I describe how to create interactive Q-Q plots using the manhattanly package. Q-Q plots tell us about the distributional assumptions of the observed test statistics and are common visualisation tools in statistical analyses.

Visit the package website for full details and example usage.

Quick Start

The following three lines of code will produce the Q-Q plot below

install.packages("manhattanly")
library(manhattanly)
qqly(HapMap, snp = "SNP", gene = "GENE")

Notice that we have added two annotations (the SNP and nearest GENE), that are revealed when hovering the mouse over a point. This feature of interactive Q-Q plots adds a great deal of information to the plot without cluttering it with text.

The Data

Inspired by the heatmaply package by Tal Galili, we split the tasks into data pre-processing and plot rendering. Therefore, we can use the manhattanly::qqr function to get the data used to produce a Q-Q plot. This allows flexibility in the rendering of the plot, since any graphics package, such as plot in base R can make used to create the plot.

The plot data is derived using the manhattanly::qqr function:

qqrObject <- qqr(HapMap)
str(qqrObject)

## List of 6 ## $ data :'data.frame': 14412 obs. of 3 variables: ## ..$ P : num [1:14412] 6.75e-10 3.41e-09 3.95e-09 4.71e-09 5.02e-09 ... ## ..$ OBSERVED: num [1:14412] 9.17 8.47 8.4 8.33 8.3 ... ## ..$ EXPECTED: num [1:14412] 4.46 3.98 3.76 3.61 3.51 ... ## $ pName : chr "P" ## $ snpName : logi NA ## $ geneName : logi NA ## $ annotation1Name: logi NA ## $ annotation2Name: logi NA ## - attr(*, "class")= chr "qqr"

head(qqrObject[["data"]])

## P OBSERVED EXPECTED ## 4346 6.75010e-10 9.170690 4.459754 ## 4347 3.41101e-09 8.467117 3.982633 ## 4344 3.95101e-09 8.403292 3.760784 ## 4338 4.70701e-09 8.327255 3.614656 ## 4342 5.02201e-09 8.299122 3.505512 ## 4341 6.22801e-09 8.205651 3.418362

This qqrObject which is of class qqr can also be passed to the manhattanly::qqly function to produce the inteactive Q-Q plot above:

qqly(qqrObject)

Related Work

This work is based on the qqman package by Stephen Turner. It produces similar manhattan and Q-Q plots as the qqman::manhattan and qqman::qq functions; the main difference here is being able to interact with the plot, including extra annotation information and seamless integration with HTML.

To leave a comment for the author, please follow the link and comment on their blog: R – Modern Data.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)