— Dave Tang (@davetang31) May 13, 2016
I finally gave it a go when a friend asked me for help making a Gantt chart and I was impressed with plotly’s interactivity and ease of use. Since I use scatter plots a lot, this post will be about making interactive scatter plots in R using plotly.
Six years ago I learned of rggobi and that was my means of creating interactive scatter plots. However, since switching from Windows to OS X, I couldn’t get rggobi working on OS X. I settled with using R’s identify() function to identify points and used different colours to highlight points of interest.
Creating interactive plots in R using plotly is incredibly simple; the syntax is similar to qplot() from the ggplot2 package. I’ll use ExAC’s functional gene constraint data to create a scatter plot; if you are interested in the data, check out ExAC’s FAQ.
# download data wget ftp://ftp.broadinstitute.org/pub/ExAC_release/release0.3.1/functional_gene_constraint/fordist_cleaned_exac_r03_march16_z_pli_rec_null_data.txt # always compress flat files gzip fordist_cleaned_exac_r03_march16_z_pli_rec_null_data.txt
Now to create the plot.
# install if necessary install.packages('plotly') # load library library(plotly) # store as data frame df <- read.table("fordist_cleaned_exac_r03_march16_z_pli_rec_null_data.txt.gz", header = TRUE, sep = "\t", stringsAsFactors = FALSE ) # remove single outlier no_ttn <- df[df$gene!='TTN',] # plot plot_ly(data = no_ttn, x = exp_lof, y = n_lof, mode = "markers", text = gene, color = pLI)
The above plot doesn’t show the interactivity but I uploaded the plot to RPubs and you can interact with it at http://rpubs.com/davetang/205147. If you hover over a specific point, you can find out the corresponding gene; you can also zoom into specific regions and pan around. You can find out more about plotly at their site.
This work is licensed under a Creative Commons
Attribution 4.0 International License.