Part22 Interactive plots with {plotly}

{plotly} is a package that makes interactive graphs.

There are many applications to plotly, but a simple one is to convert a plot created by ggplot into an interactive version.

First we can install plotly (bottom-right panel -> Packages -> Install).

And then we load the package: tick the package in the list.


Let’s read again one of our first data file:

geneexp <- read_csv(file="GSE277039/DEG_counts_sample.csv")

We can first save the “ggplot” as an object myplot:

myplot <- ggplot(data=geneexp, mapping=aes(x=WT1, y=KO1, label=GeneSymbol, color=DE)) + 
  geom_point()

Load {plotly} package:

library(plotly)

Use the object as an input of the ggplotly() function.

ggplotly(p=myplot)

The interactive plot should appear in the Viewer tab (bottom-right panel). Not that you can zoom into the plot!

Customize what appears in the tooltip box. For example, here, we only show the gene label information:

ggplotly(p=myplot, tooltip="label")

It also works for different types of plots: for example, barplots.

mybarplot <- ggplot(data=geneexp, mapping=aes(x=DE)) + 
  geom_bar()
ggplotly(p=mybarplot)

You can add ggplot2 layers / components:

mybarplot <- ggplot(data=geneexp, mapping=aes(x=DE)) + 
  geom_bar() +
  ggtitle(label="barplot in ggplotly") +
  theme_bw()
ggplotly(p=mybarplot)